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

CategoryGroup::cmdDeleteCategoryGroup()   C

Complexity

Conditions 13
Paths 60

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 24
nc 60
nop 0

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