Completed
Push — master ( eedef1...85765a )
by Gabriel
02:55
created

HasActionTrait::hasAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.9849

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 6
c 1
b 0
f 1
nc 4
nop 0
dl 0
loc 11
ccs 3
cts 7
cp 0.4286
crap 6.9849
rs 10
1
<?php /** @noinspection PhpDocMissingThrowsInspection */
2
3
namespace Nip\Dispatcher\Commands\Traits;
4
5
/**
6
 * Trait HasActionTrait
7
 * @package Nip\Dispatcher\Commands\Traits
8
 */
9
trait HasActionTrait
10
{
11
    protected $action = null;
12
13
    /**
14
     * @return mixed
15
     */
16 7
    public function getAction()
17
    {
18 7
        return $this->action;
19
    }
20
21
    /**
22
     * @param mixed $action
23
     */
24 3
    public function setAction($action)
25
    {
26 3
        $this->action = $action;
27 3
    }
28
29
    /**
30
     * @return bool
31
     */
32 5
    public function hasAction()
33
    {
34 5
        if ($this->action === null) {
35 5
            return false;
36
        }
37
        if (is_array($this->action)) {
38
            if (!isset($this->action['action'])) {
39
                return false;
40
            }
41
        }
42
        return true;
43
    }
44
45
    /**
46
     * @param $name
47
     * @param $value
48
     * @return void
49
     */
50 5
    public function setActionParam($name, $value)
51
    {
52
        /** @noinspection PhpUnhandledExceptionInspection */
53 5
        $this->guardActionAsArray();
54
55 4
        $this->action[$name] = $value;
56 4
    }
57
58
    /**
59
     * @param $name
60
     * @param $value
61
     * @return mixed
62
     * @throws \Exception
63
     */
64 6
    public function getActionParam($name)
65
    {
66 6
        $this->guardActionAsArray();
67
68 5
        return isset($this->action[$name]) ? $this->action[$name] : null;
69
    }
70
71
    /**
72
     * @throws \Exception
73
     */
74 7
    protected function guardActionAsArray()
75
    {
76 7
        if (!is_array($this->action) && $this->action !== null) {
77 2
            throw new \Exception(
78 2
                "Command Action is not an array, [" . print_r($this->action, true) . "]"
79
            );
80
        }
81 5
    }
82
83
    /**
84
     * @param $name
85
     * @return boolean
86
     */
87 4
    public function hasActionParam($name)
88
    {
89 4
        if (is_array($this->action)) {
90 3
            return isset($this->action[$name]);
91
        }
92 1
        return false;
93
    }
94
}
95