Completed
Push — master ( 44d9ca...67ee26 )
by Armando
04:46
created

Action::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 4
nop 1
1
<?php declare(strict_types = 1);
2
/**
3
 * This file is part of the TelegramBotManager package.
4
 *
5
 * (c) Armando Lüscher <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NPM\TelegramBotManager;
12
13
class Action
14
{
15
    /**
16
     * @var array List of valid actions that can be called.
17
     */
18
    private static $valid_actions = [
19
        'set',
20
        'unset',
21
        'reset',
22
        'handle',
23
    ];
24
25
    /**
26
     * @var string Action to be executed.
27
     */
28
    private $action;
29
30
    /**
31
     * Action constructor.
32
     *
33
     * @param string $action
34
     *
35
     * @throws \InvalidArgumentException
36
     */
37
    public function __construct($action = 'handle')
38
    {
39
        $this->action = $action ?: 'handle';
40
41
        if (!$this->isAction(self::$valid_actions)) {
42
            throw new \InvalidArgumentException('Invalid action: ' . $this->action);
43
        }
44
    }
45
46
    /**
47
     * Check if the current action is one of the passed ones.
48
     *
49
     * @param string|array $actions
50
     *
51
     * @return bool
52
     */
53
    public function isAction($actions): bool
54
    {
55
        return in_array($this->action, (array)$actions, true);
56
    }
57
58
    /**
59
     * Return the current action.
60
     *
61
     * @return string
62
     */
63
    public function getAction(): string
64
    {
65
        return $this->action;
66
    }
67
68
    /**
69
     * Return a list of valid actions.
70
     *
71
     * @return array
72
     */
73
    public static function getValidActions(): array
74
    {
75
        return self::$valid_actions;
76
    }
77
}
78