Collection::cmdDeleteCollection()   C
last analyzed

Complexity

Conditions 13
Paths 72

Size

Total Lines 53
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 6.3327
c 0
b 0
f 0
cc 13
eloc 28
nc 72
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\commands;
11
12
use gplcart\core\models\Collection as CollectionModel;
13
use gplcart\modules\cli\controllers\Command;
14
15
/**
16
 * Handles commands related to collections
17
 */
18
class Collection extends Command
19
{
20
21
    /**
22
     * Collection model instance
23
     * @var \gplcart\core\models\Collection $collection
24
     */
25
    protected $collection;
26
27
    /**
28
     * @param CollectionModel $collection
29
     */
30
    public function __construct(CollectionModel $collection)
31
    {
32
        parent::__construct();
33
34
        $this->collection = $collection;
35
    }
36
37
    /**
38
     * Callback for "collection-get" command
39
     */
40
    public function cmdGetCollection()
41
    {
42
        $result = $this->getListCollection();
43
        $this->outputFormat($result);
44
        $this->outputFormatTableCollection($result);
45
        $this->output();
46
    }
47
48
    /**
49
     * Callback for "collection-delete" command
50
     */
51
    public function cmdDeleteCollection()
52
    {
53
        $id = $this->getParam(0);
54
        $all = $this->getParam('all');
55
56
        if (empty($id) && empty($all)) {
57
            $this->errorAndExit($this->text('Invalid command'));
58
        }
59
60
        $options = null;
61
62
        if (isset($id)) {
63
64
            if ($this->getParam('type')) {
65
                $options = array('type' => $id);
66
            } else if ($this->getParam('store')) {
67
68
                if (!is_numeric($id)) {
69
                    $this->errorAndExit($this->text('Invalid argument'));
70
                }
71
72
                $options = array('store_id' => $id);
73
            }
74
75
        } else if (!empty($all)) {
76
            $options = array();
77
        }
78
79
        if (isset($options)) {
80
81
            $deleted = $count = 0;
82
            foreach ($this->collection->getList($options) as $item) {
83
                $count++;
84
                $deleted += (int) $this->collection->delete($item['collection_id']);
85
            }
86
87
            $result = $count && $count == $deleted;
88
89
        } else {
90
91
            if (!is_numeric($id)) {
92
                $this->errorAndExit($this->text('Invalid argument'));
93
            }
94
95
            $result = $this->collection->delete($id);
96
        }
97
98
        if (empty($result)) {
99
            $this->errorAndExit($this->text('Unexpected result'));
100
        }
101
102
        $this->output();
103
    }
104
105
    /**
106
     * Callback for "collection-add" command
107
     */
108
    public function cmdAddCollection()
109
    {
110
        if ($this->getParam()) {
111
            $this->submitAddCollection();
112
        } else {
113
            $this->wizardAddCollection();
114
        }
115
116
        $this->output();
117
    }
118
119
    /**
120
     * Callback for "collection-update" command
121
     */
122
    public function cmdUpdateCollection()
123
    {
124
        $params = $this->getParam();
125
126
        if (empty($params[0]) || count($params) < 2) {
127
            $this->errorAndExit($this->text('Invalid command'));
128
        }
129
130
        if (!is_numeric($params[0])) {
131
            $this->errorAndExit($this->text('Invalid argument'));
132
        }
133
134
        $this->setSubmitted(null, $params);
135
        $this->setSubmitted('update', $params[0]);
136
        $this->validateComponent('collection');
137
138
        $this->updateCollection($params[0]);
139
        $this->output();
140
    }
141
142
    /**
143
     * Returns an array of collections
144
     * @return array
145
     */
146
    protected function getListCollection()
147
    {
148
        $id = $this->getParam(0);
149
150
        if (!isset($id)) {
151
            return $this->collection->getList(array('limit' => $this->getLimit()));
152
        }
153
154
        if ($this->getParam('type')) {
155
            return $this->collection->getList(array('type' => $id, 'limit' => $this->getLimit()));
156
        }
157
158
        if (!is_numeric($id)) {
159
            $this->errorAndExit($this->text('Invalid argument'));
160
        }
161
162
        if ($this->getParam('store')) {
163
            return $this->collection->getList(array('store_id' => $id, 'limit' => $this->getLimit()));
164
        }
165
166
        $result = $this->collection->get($id);
167
168
        if (empty($result)) {
169
            $this->errorAndExit($this->text('Unexpected result'));
170
        }
171
172
        return array($result);
173
    }
174
175
    /**
176
     * Output table format
177
     * @param array $items
178
     */
179
    protected function outputFormatTableCollection(array $items)
180
    {
181
        $header = array(
182
            $this->text('ID'),
183
            $this->text('Name'),
184
            $this->text('Type'),
185
            $this->text('Store'),
186
            $this->text('Enabled')
187
        );
188
189
        $rows = array();
190
191
        foreach ($items as $item) {
192
            $rows[] = array(
193
                $item['collection_id'],
194
                $item['title'],
195
                $item['type'],
196
                $item['store_id'],
197
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
198
            );
199
        }
200
201
        $this->outputFormatTable($rows, $header);
202
    }
203
204
    /**
205
     * Add a new collection
206
     */
207
    protected function addCollection()
208
    {
209
        if (!$this->isError()) {
210
            $id = $this->collection->add($this->getSubmitted());
211
            if (empty($id)) {
212
                $this->errorAndExit($this->text('Unexpected result'));
213
            }
214
            $this->line($id);
215
        }
216
    }
217
218
    /**
219
     * Updates a collection
220
     * @param string $collection_id
221
     */
222
    protected function updateCollection($collection_id)
223
    {
224
        if (!$this->isError() && !$this->collection->update($collection_id, $this->getSubmitted())) {
225
            $this->errorAndExit($this->text('Unexpected result'));
226
        }
227
    }
228
229
    /**
230
     * Add a new collection at once
231
     */
232
    protected function submitAddCollection()
233
    {
234
        $this->setSubmitted(null, $this->getParam());
235
        $this->validateComponent('collection');
236
        $this->addCollection();
237
    }
238
239
    /**
240
     * Add a new collection step by step
241
     */
242
    protected function wizardAddCollection()
243
    {
244
        $this->validatePrompt('title', $this->text('Title'), 'collection');
245
        $this->validateMenu('type', $this->text('Type'), 'collection', $this->collection->getTypes());
246
        $this->validatePrompt('store_id', $this->text('Store'), 'collection');
247
        $this->validatePrompt('description', $this->text('Description'), 'collection', '');
248
        $this->validatePrompt('status', $this->text('Status'), 'collection', 0);
249
250
        $this->validateComponent('collection');
251
        $this->addCollection();
252
    }
253
254
}
255