Completed
Push — master ( ca4c8c...687844 )
by Sebastian
03:10
created

Action::getConditionJsonData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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