← All changes
|
app/Features/Notifications/Notices/AbstractNotice.php
+141
-0
2.0.0
→
2.1.1
View file →
| @@ -1,0 +1,141 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Features\Notifications\Notices; | |
| 6 | + | |
| 7 | +use Metricool\Interfaces\NoticeInterface; | |
| 8 | + | |
| 9 | +abstract class AbstractNotice implements NoticeInterface | |
| 10 | +{ | |
| 11 | + public const TYPE_INFO = 'info'; | |
| 12 | + public const TYPE_WARNING = 'warning'; | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * This constant is used to show the notice on all routes. | |
| 16 | + */ | |
| 17 | + public const ALL_ROUTES = '/'; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Override this constant to define the identifier of the Notice. This | |
| 21 | + * identifier is used to identify the Notice in the database and in the UI. | |
| 22 | + */ | |
| 23 | + public const IDENTIFIER = ''; | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * Override this property to define the version of the Notice. This version is | |
| 27 | + * used to determine if the Notice should be upgraded during a plugin update. | |
| 28 | + */ | |
| 29 | + protected string $version; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Use this property to define if the Notice is a premium Notice. This is used | |
| 33 | + * as an alternative status when reading the Notice as an array. Useful for | |
| 34 | + * the UI. | |
| 35 | + */ | |
| 36 | + protected bool $premium; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Use this property to define if the Notice is active based on a | |
| 40 | + * server-side condition. By default, a notice can activate based on a | |
| 41 | + * client-side condition. | |
| 42 | + */ | |
| 43 | + protected bool $active; | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Override this method to define the title that should be displayed to the | |
| 47 | + * user in the Notices dashboard component | |
| 48 | + * @abstract | |
| 49 | + */ | |
| 50 | + abstract public function getTitle(): string; | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * Override this method to define the text that should be displayed to the | |
| 54 | + * user in the Notices dashboard component | |
| 55 | + * @abstract | |
| 56 | + */ | |
| 57 | + abstract public function getText(): string; | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Use this method to define the route on which the Notice should be | |
| 61 | + * displayed. | |
| 62 | + * @abstract | |
| 63 | + */ | |
| 64 | + abstract public function getRoute(): string; | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * @inheritDoc | |
| 68 | + */ | |
| 69 | + public function setActive(bool $state = false): void | |
| 70 | + { | |
| 71 | + $this->active = $state; | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Override this method to set the notice as active based on a server-side | |
| 76 | + * condition. By default, the notice is not active. | |
| 77 | + */ | |
| 78 | + public function isActive(): bool | |
| 79 | + { | |
| 80 | + return $this->active ?? false; | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * @inheritDoc | |
| 85 | + */ | |
| 86 | + public function getId(): string | |
| 87 | + { | |
| 88 | + return static::IDENTIFIER; | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * @inheritDoc | |
| 93 | + */ | |
| 94 | + public function getVersion(): string | |
| 95 | + { | |
| 96 | + return $this->version ?? '1.0.0'; | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * @inheritDoc | |
| 101 | + */ | |
| 102 | + public function getAction(): array | |
| 103 | + { | |
| 104 | + return []; | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Use this method to set the type of notice. By default, the type is | |
| 109 | + * 'info' but you can override this method to set the type according to your | |
| 110 | + * needs. | |
| 111 | + */ | |
| 112 | + public function getType(): string | |
| 113 | + { | |
| 114 | + return self::TYPE_INFO; | |
| 115 | + } | |
| 116 | + | |
| 117 | + /** | |
| 118 | + * Reads if the Notice is premium | |
| 119 | + */ | |
| 120 | + public function isPremium(): bool | |
| 121 | + { | |
| 122 | + return $this->premium ?? false; | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * @inheritDoc | |
| 127 | + */ | |
| 128 | + public function toArray(): array | |
| 129 | + { | |
| 130 | + return [ | |
| 131 | + 'id' => $this->getId(), | |
| 132 | + 'active' => $this->isActive(), | |
| 133 | + 'title' => $this->getTitle(), | |
| 134 | + 'text' => $this->getText(), | |
| 135 | + 'premium' => $this->isPremium(), | |
| 136 | + 'type' => $this->getType(), | |
| 137 | + 'route' => $this->getRoute(), | |
| 138 | + 'action' => $this->getAction(), | |
| 139 | + ]; | |
| 140 | + } | |
| 141 | +} | |