Completed
Push — master ( 35e61c...496f6c )
by Iurii
01:33
created

Store::setStatusStore()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 4.909
c 0
b 0
f 0
cc 9
eloc 18
nc 16
nop 1
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\Store as StoreModel;
13
14
/**
15
 * Handles commands related to stores
16
 */
17
class Store extends Base
18
{
19
20
    /**
21
     * Store model instance
22
     * @var \gplcart\core\models\Store $store
23
     */
24
    protected $store;
25
26
    /**
27
     * @param StoreModel $store
28
     */
29
    public function __construct(StoreModel $store)
30
    {
31
        parent::__construct();
32
33
        $this->store = $store;
34
    }
35
36
    /**
37
     * Callback for "store-on" command
38
     */
39
    public function cmdOnStore()
40
    {
41
        $this->setStatusStore(true);
42
        $this->output();
43
    }
44
45
    /**
46
     * Callback for "store-off" command
47
     */
48
    public function cmdOffStore()
49
    {
50
        $this->setStatusStore(false);
51
        $this->output();
52
    }
53
54
    /**
55
     * Callback for "store-get" command
56
     */
57
    public function cmdGetStore()
58
    {
59
        $result = $this->getListStore();
60
        $this->outputFormat($result);
61
        $this->outputFormatTableStore($result);
62
        $this->output();
63
    }
64
65
    /**
66
     * Callback for "store-delete" command
67
     */
68
    public function cmdDeleteStore()
69
    {
70
        $id = $this->getParam(0);
71
72
        if (empty($id) || !is_numeric($id)) {
73
            $this->errorAndExit($this->text('Invalid ID'));
74
        }
75
76
        if (!$this->store->delete($id)) {
77
            $this->errorAndExit($this->text('An error occurred'));
78
        }
79
80
        $this->output();
81
    }
82
83
    /**
84
     * Callback for "store-add" command
85
     */
86
    public function cmdAddStore()
87
    {
88
        if ($this->getParam()) {
89
            $this->submitAddStore();
90
        } else {
91
            $this->wizardAddStore();
92
        }
93
94
        $this->output();
95
    }
96
97
    /**
98
     * Callback for "store-update" command
99
     */
100
    public function cmdUpdateStore()
101
    {
102
        $params = $this->getParam();
103
104
        if (empty($params[0]) || count($params) < 2) {
105
            $this->errorAndExit($this->text('Invalid command'));
106
        }
107
108
        if (!is_numeric($params[0])) {
109
            $this->errorAndExit($this->text('Invalid ID'));
110
        }
111
112
        $this->setSubmitted(null, $params);
113
        $this->setSubmitted('update', $params[0]);
114
        $this->setSubmittedJson('data');
115
116
        $this->validateComponent('store');
117
118
        $this->updateStore($params[0]);
119
        $this->output();
120
    }
121
122
    /**
123
     * Sets status for one or several stores
124
     * @param bool $status
125
     */
126
    protected function setStatusStore($status)
127
    {
128
        $id = $this->getParam(0);
129
        $all = $this->getParam('all');
130
131
        if (!isset($id) && empty($all)) {
132
            $this->errorAndExit($this->text('Invalid command'));
133
        }
134
135
        $result = false;
136
137
        if (isset($id)) {
138
139
            if (!is_numeric($id)) {
140
                $this->errorAndExit($this->text('Invalid ID'));
141
            }
142
143
            $result = $this->store->update($id, array('status' => $status));
144
145
        } else if (!empty($all)) {
146
147
            $updated = $count = 0;
148
            foreach ((array) $this->store->getList() as $store) {
149
                $count++;
150
                $updated += (int) $this->store->update($store['store_id'], array('status' => $status));
151
            }
152
153
            $result = $count && $count == $updated;
154
        }
155
156
        if (!$result) {
157
            $this->errorAndExit($this->text('An error occurred'));
158
        }
159
    }
160
161
    /**
162
     * Returns an array of stores
163
     * @return array
164
     */
165
    protected function getListStore()
166
    {
167
        $id = $this->getParam(0);
168
169
        if (!isset($id)) {
170
            return $this->store->getList(array('limit' => $this->getLimit()));
171
        }
172
173
        if (!is_numeric($id)) {
174
            $this->errorAndExit($this->text('Invalid ID'));
175
        }
176
177
        $result = $this->store->get($id);
178
179
        if (empty($result)) {
180
            $this->errorAndExit($this->text('Invalid ID'));
181
        }
182
183
        return array($result);
184
    }
185
186
    /**
187
     * Output table format
188
     * @param array $items
189
     */
190
    protected function outputFormatTableStore(array $items)
191
    {
192
        $header = array(
193
            $this->text('ID'),
194
            $this->text('Name'),
195
            $this->text('Domain'),
196
            $this->text('Path'),
197
            $this->text('Enabled')
198
        );
199
200
        $rows = array();
201
202
        foreach ($items as $item) {
203
            $rows[] = array(
204
                $item['store_id'],
205
                $item['name'],
206
                $item['domain'],
207
                $item['basepath'],
208
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
209
            );
210
        }
211
212
        $this->outputFormatTable($rows, $header);
213
    }
214
215
    /**
216
     * Updates a store
217
     * @param string $store_id
218
     */
219
    protected function updateStore($store_id)
220
    {
221
        if (!$this->isError() && !$this->store->update($store_id, $this->getSubmitted())) {
222
            $this->errorAndExit($this->text('An error occurred'));
223
        }
224
    }
225
226
    /**
227
     * Add a new store at once
228
     */
229
    protected function submitAddStore()
230
    {
231
        $this->setSubmitted(null, $this->getParam());
232
        $this->setSubmittedJson('data');
233
234
        $this->validateComponent('store');
235
        $this->addStore();
236
    }
237
238
    /**
239
     * Add a store
240
     */
241
    protected function addStore()
242
    {
243
        if (!$this->isError()) {
244
            $id = $this->store->add($this->getSubmitted());
245
            if (empty($id)) {
246
                $this->errorAndExit($this->text('An error occurred'));
247
            }
248
            $this->line($id);
249
        }
250
    }
251
252
    /**
253
     * Add a new store step by step
254
     */
255
    protected function wizardAddStore()
256
    {
257
        $this->validatePrompt('name', $this->text('Name'), 'store');
258
        $this->validatePrompt('domain', $this->text('Domain or IP'), 'store');
259
        $this->validatePrompt('basepath', $this->text('Path'), 'store', '');
260
        $this->validatePrompt('status', $this->text('Status'), 'store', 0);
261
        $this->setSubmittedJson('data');
262
263
        $this->validateComponent('store');
264
        $this->addStore();
265
    }
266
}
267