| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Metricool\Features\TaskManagement\Tasks; |
| 6 |
|
| 7 |
use Metricool\Interfaces\TaskInterface; |
| 8 |
use Metricool\Features\TaskManagement\Exceptions\DismissRequiredTaskException; |
| 9 |
|
| 10 |
abstract class AbstractTask implements TaskInterface |
| 11 |
{ |
| 12 |
public const DEFAULT_PRIORITY = 10; |
| 13 |
|
| 14 |
public const STATUS_URGENT = 'urgent'; |
| 15 |
public const STATUS_OPEN = 'open'; |
| 16 |
public const STATUS_COMPLETED = 'completed'; |
| 17 |
public const STATUS_DISMISSED = 'dismissed'; |
| 18 |
public const STATUS_HIDDEN = 'hidden'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Statuses that are used to determine the state of the task. A state holds |
| 22 |
* properties that are used for a task with this status. |
| 23 |
*/ |
| 24 |
protected const STATUS_PRIORITY = [ |
| 25 |
self::STATUS_URGENT => 0, |
| 26 |
self::STATUS_OPEN => 10, |
| 27 |
self::STATUS_COMPLETED => 20, |
| 28 |
self::STATUS_DISMISSED => 30, |
| 29 |
self::STATUS_HIDDEN => 40, |
| 30 |
]; |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Override this constant to define the identifier of the task. This |
| 35 |
* identifier is used to identify the task in the database and in the UI. |
| 36 |
*/ |
| 37 |
public const IDENTIFIER = ''; |
| 38 |
|
| 39 |
/** |
| 40 |
* Override this property to define the version of the task. This version is |
| 41 |
* used to determine if the task should be upgraded during a plugin update. |
| 42 |
*/ |
| 43 |
protected string $version; |
| 44 |
|
| 45 |
/** |
| 46 |
* Override this property to define if the task is required or not. If the |
| 47 |
* task is required, the user will not be able to dismiss the task. |
| 48 |
*/ |
| 49 |
protected bool $required; |
| 50 |
|
| 51 |
/** |
| 52 |
* Override this property to define if the task should be reactivated when |
| 53 |
* the task is upgraded. This is useful for tasks that are dismissed by the |
| 54 |
* user but should be reactivated when the task is upgraded to a new |
| 55 |
* version. |
| 56 |
*/ |
| 57 |
protected bool $reactivateOnUpgrade; |
| 58 |
|
| 59 |
/** |
| 60 |
* Use this property to define if the task is a premium task. Useful for |
| 61 |
* the UI. |
| 62 |
*/ |
| 63 |
protected bool $premium; |
| 64 |
|
| 65 |
/** |
| 66 |
* Use this property to define if the task is related to a special feature |
| 67 |
* or not. Useful for the UI. |
| 68 |
*/ |
| 69 |
protected bool $specialFeature; |
| 70 |
|
| 71 |
/** |
| 72 |
* By default, a task is active on construct. This is because the $status |
| 73 |
* property is not set. The {@see getStatus()} method will therefore return |
| 74 |
* the default status 'open'. If you want to set a different default status |
| 75 |
* use the {@see setStatus()} method in the construct of the task. See |
| 76 |
* {@see AddMandatoryProviderTask} for an example. |
| 77 |
*/ |
| 78 |
protected string $status; |
| 79 |
|
| 80 |
/** |
| 81 |
* Override this method to define the text that should be displayed to the |
| 82 |
* user in the tasks dashboard component |
| 83 |
* @abstract |
| 84 |
*/ |
| 85 |
abstract public function getText(): string; |
| 86 |
|
| 87 |
/** |
| 88 |
* @inheritDoc |
| 89 |
*/ |
| 90 |
public function getId(): string |
| 91 |
{ |
| 92 |
return static::IDENTIFIER; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @inheritDoc |
| 97 |
*/ |
| 98 |
public function getVersion(): string |
| 99 |
{ |
| 100 |
return $this->version ?? '1.0.0'; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @inheritDoc |
| 105 |
*/ |
| 106 |
public function getStatus(): string |
| 107 |
{ |
| 108 |
return $this->status ?? self::STATUS_OPEN; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @inheritDoc |
| 113 |
*/ |
| 114 |
public function getPriority(): int |
| 115 |
{ |
| 116 |
// Get the priority from the status |
| 117 |
$priority = $this->getPriorityFromStatus(); |
| 118 |
|
| 119 |
// Give premium and special features a higher priority |
| 120 |
if ($this->isPremium() || $this->isSpecialFeature()) { |
| 121 |
$priority++; |
| 122 |
} |
| 123 |
|
| 124 |
return $priority; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Build the label for the task. This is used to display the task in the |
| 129 |
* tasks dashboard component. The label is used to indicate if the task |
| 130 |
* is premium or a special feature. If not, the label reflects the status. |
| 131 |
* Override with get{Status}Label method, for example: getUrgentLabel() |
| 132 |
*/ |
| 133 |
public function getLabel(): string |
| 134 |
{ |
| 135 |
$status = $this->getStatus(); |
| 136 |
|
| 137 |
// Get the label from method if it exists |
| 138 |
$getStatusLabelMethod = 'get' . ucfirst($status) . 'StatusLabel'; |
| 139 |
if (method_exists($this, $getStatusLabelMethod)) { |
| 140 |
return $this->$getStatusLabelMethod(); |
| 141 |
} |
| 142 |
|
| 143 |
return ucfirst($status); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get the label for the status 'open' |
| 148 |
*/ |
| 149 |
public function getOpenStatusLabel(): string |
| 150 |
{ |
| 151 |
if ($this->isPremium()) { |
| 152 |
return __('Premium', 'metricool'); |
| 153 |
} |
| 154 |
|
| 155 |
if ($this->isSpecialFeature()) { |
| 156 |
return __('Special feature', 'metricool'); |
| 157 |
} |
| 158 |
|
| 159 |
return __('Open', 'metricool'); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get the label for the status 'urgent' |
| 164 |
*/ |
| 165 |
public function getUrgentStatusLabel(): string |
| 166 |
{ |
| 167 |
return __('Urgent', 'metricool'); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get the label for the status 'completed' |
| 172 |
*/ |
| 173 |
public function getCompletedStatusLabel(): string |
| 174 |
{ |
| 175 |
return __('Completed', 'metricool'); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Get the label for the status 'dismissed' |
| 180 |
*/ |
| 181 |
public function getDismissedStatusLabel(): string |
| 182 |
{ |
| 183 |
return __('Dismissed', 'metricool'); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* @inheritDoc |
| 188 |
*/ |
| 189 |
public function getAction(): array |
| 190 |
{ |
| 191 |
return []; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Sets the premium property of the task. |
| 196 |
*/ |
| 197 |
public function setPremium(bool $isPremium): void |
| 198 |
{ |
| 199 |
$this->premium = $isPremium; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Sets the special feature property of the task. |
| 204 |
*/ |
| 205 |
public function setSpecialFeature(bool $isSpecialFeature): void |
| 206 |
{ |
| 207 |
$this->specialFeature = $isSpecialFeature; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @inheritDoc |
| 212 |
*/ |
| 213 |
public function setStatusFromTask(TaskInterface $task): self |
| 214 |
{ |
| 215 |
return $this->setStatus($task->getStatus()); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* @inheritDoc |
| 220 |
*/ |
| 221 |
public function open(): self |
| 222 |
{ |
| 223 |
return $this->setStatus(self::STATUS_OPEN); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* @inheritDoc |
| 228 |
*/ |
| 229 |
public function urgent(): self |
| 230 |
{ |
| 231 |
return $this->setStatus(self::STATUS_URGENT); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* @inheritDoc |
| 236 |
*/ |
| 237 |
public function dismiss(): self |
| 238 |
{ |
| 239 |
if (!$this->isDismissed() && $this->isRequired()) { |
| 240 |
throw new DismissRequiredTaskException(); |
| 241 |
} |
| 242 |
|
| 243 |
return $this->setStatus(self::STATUS_DISMISSED); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* @inheritDoc |
| 248 |
*/ |
| 249 |
public function complete(): self |
| 250 |
{ |
| 251 |
return $this->setStatus(self::STATUS_COMPLETED); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* @inheritDoc |
| 256 |
*/ |
| 257 |
public function hide(): self |
| 258 |
{ |
| 259 |
return $this->setStatus(self::STATUS_HIDDEN); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Reads if the task is completed |
| 264 |
*/ |
| 265 |
public function isCompleted(): bool |
| 266 |
{ |
| 267 |
return $this->getStatus() === self::STATUS_COMPLETED; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Reads if the task is dismissed |
| 272 |
*/ |
| 273 |
public function isDismissed(): bool |
| 274 |
{ |
| 275 |
return $this->getStatus() === self::STATUS_DISMISSED; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* @inheritDoc |
| 280 |
*/ |
| 281 |
public function isHidden(): bool |
| 282 |
{ |
| 283 |
return $this->getStatus() === self::STATUS_HIDDEN; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* @inheritDoc |
| 288 |
*/ |
| 289 |
public function isRequired(): bool |
| 290 |
{ |
| 291 |
return $this->required ?? false; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Reads if the task is premium |
| 296 |
*/ |
| 297 |
public function isPremium(): bool |
| 298 |
{ |
| 299 |
return $this->premium ?? false; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Reads if the task is related to a special feature |
| 304 |
*/ |
| 305 |
public function isSpecialFeature(): bool |
| 306 |
{ |
| 307 |
return $this->specialFeature ?? false; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* @inheritDoc |
| 312 |
*/ |
| 313 |
public function isReactivateOnUpgrade(): bool |
| 314 |
{ |
| 315 |
return $this->reactivateOnUpgrade ?? false; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Sets the status of the task. |
| 320 |
*/ |
| 321 |
private function setStatus(string $status): self |
| 322 |
{ |
| 323 |
$this->status = $status; |
| 324 |
|
| 325 |
return $this; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Returns the priority of the task based on the status. If the status is |
| 330 |
* not found in STATUS_PRIORITY, the default priority is returned. |
| 331 |
*/ |
| 332 |
protected function getPriorityFromStatus(): int |
| 333 |
{ |
| 334 |
$status = $this->getStatus(); |
| 335 |
if (!isset(self::STATUS_PRIORITY[$status])) { |
| 336 |
return self::DEFAULT_PRIORITY; |
| 337 |
} |
| 338 |
return self::STATUS_PRIORITY[$status]; |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* @inheritDoc |
| 343 |
*/ |
| 344 |
public function toArray(): array |
| 345 |
{ |
| 346 |
return [ |
| 347 |
'id' => $this->getId(), |
| 348 |
'text' => $this->getText(), |
| 349 |
'label' => $this->getLabel(), |
| 350 |
'status' => $this->getStatus(), |
| 351 |
'priority' => $this->getPriority(), |
| 352 |
'premium' => $this->isPremium(), |
| 353 |
'special_feature' => $this->isSpecialFeature(), |
| 354 |
'type' => $this->isRequired() ? 'required' : 'optional', |
| 355 |
'action' => $this->getAction(), |
| 356 |
]; |
| 357 |
} |
| 358 |
} |
| 359 |
|