Completed
Push — master ( 496f6c...fa7820 )
by Iurii
01:48
created

Trigger::cmdDeleteTrigger()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 8
nop 0
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
14
/**
15
 * Handles commands related to triggers
16
 */
17
class Trigger extends Base
18
{
19
20
    /**
21
     * Trigger model instance
22
     * @var \gplcart\core\models\Trigger $trigger
23
     */
24
    protected $trigger;
25
26
    /**
27
     * @param TriggerModel $trigger
28
     */
29
    public function __construct(TriggerModel $trigger)
30
    {
31
        parent::__construct();
32
33
        $this->trigger = $trigger;
34
    }
35
36
    /**
37
     * Callback for "trigger-get" command
38
     */
39
    public function cmdGetTrigger()
40
    {
41
        $result = $this->getListTrigger();
42
        $this->outputFormat($result);
43
        $this->outputFormatTableTrigger($result);
44
        $this->output();
45
    }
46
47
    /**
48
     * Callback for "trigger-delete" command
49
     */
50
    public function cmdDeleteTrigger()
51
    {
52
        $id = $this->getParam(0);
53
54
        if (empty($id) || !is_numeric($id)) {
55
            $this->errorAndExit($this->text('Invalid argument'));
56
        }
57
58
        if ($this->getParam('store')) {
59
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 && $count == $deleted;
67
        } else {
68
            $result = $this->trigger->delete($id);
69
        }
70
71
        if (empty($result)) {
72
            $this->errorAndExit($this->text('Unexpected result'));
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->errorAndExit($this->text('Invalid command'));
101
        }
102
103
        if (!is_numeric($params[0])) {
104
            $this->errorAndExit($this->text('Invalid argument'));
105
        }
106
107
        $this->setSubmitted(null, $params);
108
        $this->setSubmitted('data.conditions', $this->getSubmitted('conditions'));
109
        $this->setSubmittedList('data.conditions');
110
        $this->setSubmitted('update', $params[0]);
111
112
        $this->validateComponent('trigger');
113
114
        $this->updateTrigger($params[0]);
115
        $this->output();
116
    }
117
118
    /**
119
     * Returns an array of triggers
120
     * @return array
121
     */
122
    protected function getListTrigger()
123
    {
124
        $id = $this->getParam(0);
125
126
        if (!isset($id)) {
127
            return $this->trigger->getList(array('limit' => $this->getLimit()));
128
        }
129
130
        if (!is_numeric($id)) {
131
            $this->errorAndExit($this->text('Invalid argument'));
132
        }
133
134
        if ($this->getParam('store')) {
135
            return $this->trigger->getList(array('store_id' => $id, 'limit' => $this->getLimit()));
136
        }
137
138
        $result = $this->trigger->get($id);
139
140
        if (empty($result)) {
141
            $this->errorAndExit($this->text('Unexpected result'));
142
        }
143
144
        return array($result);
145
    }
146
147
    /**
148
     * Output table format
149
     * @param array $items
150
     */
151
    protected function outputFormatTableTrigger(array $items)
152
    {
153
        $header = array(
154
            $this->text('ID'),
155
            $this->text('Name'),
156
            $this->text('Store'),
157
            $this->text('Weight'),
158
            $this->text('Enabled')
159
        );
160
161
        $rows = array();
162
163
        foreach ($items as $item) {
164
            $rows[] = array(
165
                $item['trigger_id'],
166
                $item['name'],
167
                $item['store_id'],
168
                $item['weight'],
169
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
170
            );
171
        }
172
173
        $this->outputFormatTable($rows, $header);
174
    }
175
176
    /**
177
     * Add a new trigger
178
     */
179
    protected function addTrigger()
180
    {
181
        if (!$this->isError()) {
182
            $state_id = $this->trigger->add($this->getSubmitted());
183
            if (empty($state_id)) {
184
                $this->errorAndExit($this->text('Unexpected result'));
185
            }
186
            $this->line($state_id);
187
        }
188
    }
189
190
    /**
191
     * Updates a trigger
192
     * @param string $trigger_id
193
     */
194
    protected function updateTrigger($trigger_id)
195
    {
196
        if (!$this->isError() && !$this->trigger->update($trigger_id, $this->getSubmitted())) {
197
            $this->errorAndExit($this->text('Unexpected result'));
198
        }
199
    }
200
201
    /**
202
     * Add a new trigger at once
203
     */
204
    protected function submitAddTrigger()
205
    {
206
        $this->setSubmitted(null, $this->getParam());
207
        $this->setSubmitted('data.conditions', $this->getSubmitted('conditions'));
208
        $this->setSubmittedList('data.conditions');
209
210
        $this->validateComponent('trigger');
211
        $this->addTrigger();
212
    }
213
214
    /**
215
     * Add a trigger step by step
216
     */
217
    protected function wizardAddTrigger()
218
    {
219
        $this->validatePrompt('name', $this->text('Name'), 'trigger');
220
        $this->validatePrompt('store_id', $this->text('Store'), 'trigger');
221
        $this->validatePromptList('data.conditions', $this->text('Conditions'), 'trigger');
222
        $this->validatePrompt('status', $this->text('Status'), 'trigger', 0);
223
        $this->validatePrompt('weight', $this->text('Weight'), 'trigger', 0);
224
        $this->setSubmittedList('data.conditions');
225
226
        $this->validateComponent('trigger');
227
        $this->addTrigger();
228
    }
229
230
}
231