| Total Complexity | 6 |
| Total Lines | 65 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php declare(strict_types=1); |
||
| 15 | class Action |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array List of valid actions that can be called. |
||
| 19 | */ |
||
| 20 | private static $valid_actions = [ |
||
| 21 | 'set', |
||
| 22 | 'unset', |
||
| 23 | 'reset', |
||
| 24 | 'handle', |
||
| 25 | 'cron', |
||
| 26 | 'webhookinfo', |
||
| 27 | ]; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string Action to be executed. |
||
| 31 | */ |
||
| 32 | private $action; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Action constructor. |
||
| 36 | * |
||
| 37 | * @param string $action |
||
| 38 | * |
||
| 39 | * @throws InvalidActionException |
||
| 40 | */ |
||
| 41 | public function __construct($action = 'handle') |
||
| 42 | { |
||
| 43 | $this->action = $action ?: 'handle'; |
||
| 44 | |||
| 45 | if (!$this->isAction(self::$valid_actions)) { |
||
| 46 | throw new InvalidActionException('Invalid action: ' . $this->action); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Check if the current action is one of the passed ones. |
||
| 52 | * |
||
| 53 | * @param string|array $actions |
||
| 54 | * |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | public function isAction($actions): bool |
||
| 58 | { |
||
| 59 | return in_array($this->action, (array) $actions, true); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Return the current action. |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function getAction(): string |
||
| 68 | { |
||
| 69 | return $this->action; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Return a list of valid actions. |
||
| 74 | * |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | public static function getValidActions(): array |
||
| 80 | } |
||
| 81 | } |
||
| 82 |