Passed
Pull Request — master (#56)
by Marco
02:22
created

Action   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 15
dl 0
loc 65
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isAction() 0 3 1
A getAction() 0 3 1
A getValidActions() 0 3 1
A __construct() 0 6 3
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 PhpTelegramBot\PhpTelegramBotManager;
12
13
use PhpTelegramBot\PhpTelegramBotManager\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
        '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
78
    {
79
        return self::$valid_actions;
80
    }
81
}
82