Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

Action::getConditionJsonData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
ccs 4
cts 4
cp 1
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Config;
13
14
/**
15
 * Class Action
16
 *
17
 * @package CaptainHook
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/captainhookphp/captainhook
20
 * @since   Class available since Release 0.9.0
21
 */
22
class Action
23
{
24
    /**
25
     * Action phpc lass or cli script
26
     *
27
     * @var string
28
     */
29
    private $action;
30
31
    /**
32
     * Map of options name => value
33
     *
34
     * @var \CaptainHook\App\Config\Options
35
     */
36
    private $options;
37
38
    /**
39
     * List of action conditions
40
     *
41
     * @var \CaptainHook\App\Config\Condition[]
42
     */
43
    private $conditions = [];
44
45
    /**
46
     * Action constructor
47
     *
48
     * @param  string $action
49
     * @param  array  $options
50
     * @param  array  $conditions
51
     * @throws \Exception
52 54
     */
53
    public function __construct(string $action, array $options = [], array $conditions = [])
54 54
    {
55 54
        $this->action = $action;
56 54
        $this->setupOptions($options);
57 54
        $this->setupConditions($conditions);
58
    }
59
60
    /**
61
     * Setup options
62
     *
63
     * @param array $options
64 54
     */
65
    private function setupOptions(array $options): void
66 54
    {
67 54
        $this->options = new Options($options);
68
    }
69
70
    /**
71
     * Setup action conditions
72
     *
73
     * @param array $conditions
74 54
     */
75
    private function setupConditions(array $conditions): void
76 54
    {
77 3
        foreach ($conditions as $condition) {
78
            $this->conditions[] = new Condition($condition['exec'], $condition['args'] ?? []);
79 54
        }
80
    }
81
82
    /**
83
     * Action getter
84
     *
85
     * @return string
86 1
     */
87
    public function getAction(): string
88 1
    {
89
        return $this->action;
90
    }
91
92
    /**
93
     * Return option map
94
     *
95
     * @return \CaptainHook\App\Config\Options
96 23
     */
97
    public function getOptions(): Options
98 23
    {
99
        return $this->options;
100
    }
101
102
    /**
103
     * Return condition configurations
104
     *
105
     * @return \CaptainHook\App\Config\Condition[]
106 1
     */
107
    public function getConditions(): array
108 1
    {
109
        return $this->conditions;
110
    }
111
112
    /**
113
     * Return config data
114
     *
115
     * @return array
116 6
     */
117
    public function getJsonData(): array
118
    {
119 6
        return [
120 6
            'action'     => $this->action,
121 6
            'options'    => $this->options->getAll(),
122
            'conditions' => $this->getConditionJsonData()
123
        ];
124
    }
125
126
    /**
127
     * Return conditions json data
128
     *
129
     * @return array
130 6
     */
131
    private function getConditionJsonData(): array
132 6
    {
133 6
        $json = [];
134 1
        foreach ($this->conditions as $condition) {
135
            $json[] = $condition->getJsonData();
136 6
        }
137
        return $json;
138
    }
139
}
140