Completed
Push — master ( bd324c...b4485a )
by Armando
13s
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 TelegramBot\TelegramBotManager;
12
13
use TelegramBot\TelegramBotManager\Exception\InvalidActionException;
14
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
    ];
27
28
    /**
29
     * @var string Action to be executed.
30
     */
31
    private $action;
32
33
    /**
34
     * Action constructor.
35
     *
36
     * @param string $action
37
     *
38
     * @throws \TelegramBot\TelegramBotManager\Exception\InvalidActionException
39
     */
40
    public function __construct($action = 'handle')
41
    {
42
        $this->action = $action ?: 'handle';
43
44
        if (!$this->isAction(self::$valid_actions)) {
45
            throw new InvalidActionException('Invalid action: ' . $this->action);
46
        }
47
    }
48
49
    /**
50
     * Check if the current action is one of the passed ones.
51
     *
52
     * @param string|array $actions
53
     *
54
     * @return bool
55
     */
56
    public function isAction($actions): bool
57
    {
58
        return in_array($this->action, (array) $actions, true);
59
    }
60
61
    /**
62
     * Return the current action.
63
     *
64
     * @return string
65
     */
66
    public function getAction(): string
67
    {
68
        return $this->action;
69
    }
70
71
    /**
72
     * Return a list of valid actions.
73
     *
74
     * @return array
75
     */
76
    public static function getValidActions(): array
77
    {
78
        return self::$valid_actions;
79
    }
80
}
81