Issues (39)

src/Commands/Traits/HasActionTrait.php (1 issue)

Labels
Severity
1
<?php
2
3
/** @noinspection PhpDocMissingThrowsInspection */
4
5
namespace Nip\Dispatcher\Commands\Traits;
6
7
/**
8
 * Trait HasActionTrait
9
 * @package Nip\Dispatcher\Commands\Traits
10
 */
11
trait HasActionTrait
12
{
13
    protected $action = null;
14
15
    /**
16
     * @return mixed
17
     */
18 7
    public function getAction()
19
    {
20 7
        return $this->action;
21
    }
22
23
    /**
24
     * @param mixed $action
25
     */
26 3
    public function setAction($action)
27
    {
28 3
        $this->action = $action;
29 3
    }
30
31
    /**
32
     * @return bool
33
     */
34 5
    public function hasAction()
35
    {
36 5
        if ($this->action === null) {
37 5
            return false;
38
        }
39
        if (is_array($this->action)) {
40
            if (!isset($this->action['action'])) {
41
                return false;
42
            }
43
        }
44
        return true;
45
    }
46
47
    /**
48
     * @param $name
49
     * @param $value
50
     * @return void
51
     */
52 5
    public function setActionParam($name, $value)
53
    {
54
        /** @noinspection PhpUnhandledExceptionInspection */
55 5
        $this->guardActionAsArray();
56
57 4
        $this->action[$name] = $value;
58 4
    }
59
60
    /**
61
     * @param $name
62
     * @param $value
63
     * @return mixed
64
     * @throws \Exception
65
     */
66 6
    public function getActionParam($name)
67
    {
68 6
        $this->guardActionAsArray();
69
70 5
        return isset($this->action[$name]) ? $this->action[$name] : null;
71
    }
72
73
    /**
74
     * @throws \Exception
75
     */
76 7
    protected function guardActionAsArray()
77
    {
78 7
        if (!is_array($this->action) && $this->action !== null) {
79 2
            throw new \Exception(
80 2
                "Command Action is not an array, [" . print_r($this->action, true) . "]"
0 ignored issues
show
Are you sure print_r($this->action, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
                "Command Action is not an array, [" . /** @scrutinizer ignore-type */ print_r($this->action, true) . "]"
Loading history...
81
            );
82
        }
83 5
    }
84
85
    /**
86
     * @param $name
87
     * @return boolean
88
     */
89 4
    public function hasActionParam($name)
90
    {
91 4
        if (is_array($this->action)) {
92 3
            return isset($this->action[$name]);
93
        }
94 1
        return false;
95
    }
96
}
97