Completed
Push — master ( 6c8ffa...b951a8 )
by Iurii
01:25
created

Trigger   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 1
dl 0
loc 235
rs 9.6
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A cmdGetTrigger() 0 7 1
B cmdDeleteTrigger() 0 26 6
A cmdAddTrigger() 0 10 2
A cmdUpdateTrigger() 0 18 3
B getListTrigger() 0 25 4
A outputFormatTableTrigger() 0 23 3
A updateTrigger() 0 6 3
A submitAddTrigger() 0 8 1
A addTrigger() 0 10 3
A wizardAddTrigger() 0 12 1
A validateInputConditionsTrigger() 0 11 2
A setSubmittedConditionsTrigger() 0 8 2
1
<?php
2
3
/**
4
 * @package CLI
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\cli\controllers;
11
12
use gplcart\core\models\Trigger as TriggerModel;
13
use gplcart\modules\cli\controllers\Base;
14
15
/**
16
 * Handles commands related to triggers
17
 */
18
class Trigger extends Base
19
{
20
21
    /**
22
     * Trigger model instance
23
     * @var \gplcart\core\models\Trigger $trigger
24
     */
25
    protected $trigger;
26
27
    /**
28
     * @param TriggerModel $trigger
29
     */
30
    public function __construct(TriggerModel $trigger)
31
    {
32
        parent::__construct();
33
34
        $this->trigger = $trigger;
35
    }
36
37
    /**
38
     * Callback for "trigger-get" command
39
     */
40
    public function cmdGetTrigger()
41
    {
42
        $result = $this->getListTrigger();
43
        $this->outputFormat($result);
44
        $this->outputFormatTableTrigger($result);
45
        $this->output();
46
    }
47
48
    /**
49
     * Callback for "trigger-delete" command
50
     */
51
    public function cmdDeleteTrigger()
52
    {
53
        $id = $this->getParam(0);
54
55
        if (empty($id) || !is_numeric($id)) {
56
            $this->errorExit($this->text('Invalid ID'));
57
        }
58
59
        if ($this->getParam('store')) {
60
            $deleted = $count = 0;
61
            foreach ($this->trigger->getList(array('store_id' => $id)) as $item) {
62
                $count++;
63
                $deleted += (int) $this->trigger->delete($item['trigger_id']);
64
            }
65
66
            $result = ($count == $deleted);
67
        } else {
68
            $result = $this->trigger->delete($id);
69
        }
70
71
        if (!$result) {
72
            $this->errorExit($this->text('An error occurred'));
73
        }
74
75
        $this->output();
76
    }
77
78
    /**
79
     * Callback for "trigger-add" command
80
     */
81
    public function cmdAddTrigger()
82
    {
83
        if ($this->getParam()) {
84
            $this->submitAddTrigger();
85
        } else {
86
            $this->wizardAddTrigger();
87
        }
88
89
        $this->output();
90
    }
91
92
    /**
93
     * Callback for "trigger-update" command
94
     */
95
    public function cmdUpdateTrigger()
96
    {
97
        $params = $this->getParam();
98
99
        if (empty($params[0]) || count($params) < 2) {
100
            $this->errorExit($this->text('Invalid command'));
101
        }
102
103
        $this->setSubmitted(null, $this->getParam());
104
        $this->setSubmitted('data.conditions', $this->getSubmitted('conditions'));
105
        $this->setSubmittedConditionsTrigger();
106
        $this->setSubmitted('update', $params[0]);
107
108
        $this->validateComponent('trigger');
109
        $this->updateTrigger($params[0]);
110
111
        $this->output();
112
    }
113
114
    /**
115
     * Returns an array of triggers
116
     * @return array
117
     */
118
    protected function getListTrigger()
119
    {
120
        $id = $this->getParam(0);
121
122
        if (isset($id)) {
123
124
            if ($this->getParam('store')) {
125
                $list = $this->trigger->getList(array('store_id' => $id));
126
                $this->limitItems($list);
127
                return $list;
128
            }
129
130
            $result = $this->trigger->get($id);
131
132
            if (empty($result)) {
133
                $this->errorExit($this->text('Invalid ID'));
134
            }
135
136
            return array($result);
137
        }
138
139
        $list = $this->trigger->getList();
140
        $this->limitItems($list);
141
        return $list;
142
    }
143
144
    /**
145
     * Output table format
146
     * @param array $items
147
     */
148
    protected function outputFormatTableTrigger(array $items)
149
    {
150
        $header = array(
151
            $this->text('ID'),
152
            $this->text('Name'),
153
            $this->text('Store'),
154
            $this->text('Weight'),
155
            $this->text('Enabled')
156
        );
157
158
        $rows = array();
159
        foreach ($items as $item) {
160
            $rows[] = array(
161
                $item['trigger_id'],
162
                $item['name'],
163
                $item['store_id'],
164
                $item['weight'],
165
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
166
            );
167
        }
168
169
        $this->outputFormatTable($rows, $header);
170
    }
171
172
    /**
173
     * Updates a trigger
174
     * @param string $trigger_id
175
     */
176
    protected function updateTrigger($trigger_id)
177
    {
178
        if (!$this->isError() && !$this->trigger->update($trigger_id, $this->getSubmitted())) {
179
            $this->errorExit($this->text('Trigger has not been updated'));
180
        }
181
    }
182
183
    /**
184
     * Add a new trigger at once
185
     */
186
    protected function submitAddTrigger()
187
    {
188
        $this->setSubmitted(null, $this->getParam());
189
        $this->setSubmitted('data.conditions', $this->getSubmitted('conditions'));
190
        $this->setSubmittedConditionsTrigger();
191
        $this->validateComponent('trigger');
192
        $this->addTrigger();
193
    }
194
195
    /**
196
     * Add a new trigger
197
     */
198
    protected function addTrigger()
199
    {
200
        if (!$this->isError()) {
201
            $state_id = $this->trigger->add($this->getSubmitted());
202
            if (empty($state_id)) {
203
                $this->errorExit($this->text('Trigger has not been added'));
204
            }
205
            $this->line($state_id);
206
        }
207
    }
208
209
    /**
210
     * Add a trigger step by step
211
     */
212
    protected function wizardAddTrigger()
213
    {
214
        $this->validateInput('name', $this->text('Name'), 'trigger');
215
        $this->validateInput('store_id', $this->text('Store'), 'trigger');
216
        $this->validateInputConditionsTrigger();
217
        $this->validateInput('status', $this->text('Status'), 'trigger', 0);
218
        $this->validateInput('weight', $this->text('Weight'), 'trigger', 0);
219
        $this->setSubmittedConditionsTrigger();
220
221
        $this->validateComponent('trigger');
222
        $this->addTrigger();
223
    }
224
225
    /**
226
     * Validate conditions
227
     */
228
    protected function validateInputConditionsTrigger()
229
    {
230
        $input = $this->prompt($this->text('Conditions'));
231
232
        if ($this->isValidInput($this->explodeByPipe($input), 'data.conditions', 'trigger')) {
233
            $this->setSubmitted('data.conditions', $input);
234
        } else {
235
            $this->errors();
236
            $this->validateInputConditionsTrigger();
237
        }
238
    }
239
240
    /**
241
     * Sets trigger conditions
242
     */
243
    protected function setSubmittedConditionsTrigger()
244
    {
245
        $actions = $this->getSubmitted('data.conditions');
246
247
        if (isset($actions)) {
248
            $this->setSubmitted('data.conditions', $this->explodeByPipe($actions));
249
        }
250
    }
251
252
}
253