Completed
Push — master ( 1b4550...82aca0 )
by Iurii
04:48
created

Alias::cmdDeleteAlias()   C

Complexity

Conditions 12
Paths 64

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 5.15
c 0
b 0
f 0
cc 12
eloc 24
nc 64
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\commands;
11
12
use gplcart\core\interfaces\Crud as CrudInterface;
13
use gplcart\core\models\Alias as AliasModel;
14
use gplcart\modules\cli\controllers\Command;
15
16
17
/**
18
 * Handles commands related to URL aliases
19
 */
20
class Alias extends Command
21
{
22
23
    /**
24
     * Alias model instance
25
     * @var \gplcart\core\models\Alias $alias
26
     */
27
    protected $alias;
28
29
    /**
30
     * @param AliasModel $alias
31
     */
32
    public function __construct(AliasModel $alias)
33
    {
34
        parent::__construct();
35
36
        $this->alias = $alias;
37
    }
38
39
    /**
40
     * Callback for "alias-get" command
41
     */
42
    public function cmdGetAlias()
43
    {
44
        $result = $this->getListAlias();
45
        $this->outputFormat($result);
46
        $this->outputFormatTableAlias($result);
47
        $this->output();
48
    }
49
50
    /**
51
     * Callback for "alias-add" command
52
     */
53
    public function cmdAddAlias()
54
    {
55
        $params = $this->getParam();
56
57
        if (count($params) != 3) {
58
            $this->errorAndExit($this->text('Invalid command'));
59
        }
60
61
        list($entity, $entity_id, $alias) = $params;
62
63
        if (empty($entity) || empty($entity_id) || empty($alias) || !is_numeric($entity_id)) {
64
            $this->errorAndExit($this->text('Invalid argument'));
65
        }
66
67
        if (!$this->alias->loadEntity($entity, $entity_id)) {
68
            $this->errorAndExit($this->text('Invalid argument'));
69
        }
70
71
        $result = $this->addAlias($entity, $entity_id, $alias);
72
73
        if (empty($result)) {
74
            $this->errorAndExit($this->text('Unexpected result'));
75
        }
76
77
        $this->line($result);
78
        $this->output();
79
    }
80
81
    /**
82
     * Callback for "alias-generate" command
83
     */
84
    public function cmdGenerateAlias()
85
    {
86
        $entity = $this->getParam(0);
87
        $entity_id = $this->getParam(1);
88
        $all = $this->getParam('all');
89
90
        if (empty($entity)) {
91
            $this->errorAndExit($this->text('Invalid command'));
92
        }
93
94
        if (!isset($entity_id) && empty($all)) {
95
            $this->errorAndExit($this->text('Invalid command'));
96
        }
97
98
        $result = false;
99
100
        if (isset($entity_id)) {
101
102
            if (!is_numeric($entity_id)) {
103
                $this->errorAndExit($this->text('Invalid argument'));
104
            }
105
106
            $data = $this->alias->loadEntity($entity, $entity_id);
107
108
            if (empty($data)) {
109
                $this->errorAndExit($this->text('Invalid argument'));
110
            }
111
112
            $alias = $this->alias->generateEntity($entity, $data);
113
114
            if (empty($alias)) {
115
                $this->errorAndExit($this->text('Unexpected result'));
116
            }
117
118
            $result = $this->addAlias($entity, $entity_id, $alias);
119
120
        } else if (!empty($all)) {
121
            $result = $this->generateListAlias($entity);
122
        }
123
124
        if (empty($result)) {
125
            $this->errorAndExit($this->text('Unexpected result'));
126
        }
127
128
        $this->output();
129
    }
130
131
    /** Add a URL alias
132
     * @param string $entity
133
     * @param int $entity_id
134
     * @param string $alias
135
     * @return int
136
     */
137
    protected function addAlias($entity, $entity_id, $alias)
138
    {
139
        $this->alias->delete(array('entity' => $entity, 'entity_id' => $entity_id));
140
141
        $data = array(
142
            'alias' => $alias,
143
            'entity' => $entity,
144
            'entity_id' => $entity_id
145
        );
146
147
        return $this->alias->add($data);
148
    }
149
150
    /**
151
     * Mass generate URL aliases for the given entity
152
     * @param string $entity
153
     * @return bool
154
     */
155
    protected function generateListAlias($entity)
156
    {
157
        if (!$this->alias->isSupportedEntity($entity)) {
158
            $this->errorAndExit($this->text('Invalid argument'));
159
        }
160
161
        $model = gplcart_instance_model($entity);
162
163
        if (!$model instanceof CrudInterface) {
0 ignored issues
show
Bug introduced by
The class gplcart\core\interfaces\Crud does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
164
            $this->errorAndExit($this->text('Invalid argument'));
165
        }
166
167
        ini_set('memory_limit', '-1');
168
169
        $added = $count = 0;
170
171
        foreach ($model->getList() as $item) {
172
173
            if (empty($item["{$entity}_id"])) {
174
                continue;
175
            }
176
177
            $count++;
178
            $entity_id = $item["{$entity}_id"];
179
180
            $this->alias->delete(array('entity' => $entity, 'entity_id' => $entity_id));
181
182
            $data = array(
183
                'entity' => $entity,
184
                'entity_id' => $entity_id,
185
                'alias' => $this->alias->generateEntity($entity, $item)
186
            );
187
188
            if ($this->alias->add($data)) {
189
                $added++;
190
            }
191
        }
192
193
        return $count && $count == $added;
194
    }
195
196
197
    /**
198
     * Callback for "alias-delete" command
199
     */
200
    public function cmdDeleteAlias()
201
    {
202
        $id = $this->getParam(0);
203
        $all = $this->getParam('all');
204
205
        if (!isset($id) && !$all) {
206
            $this->errorAndExit($this->text('Invalid command'));
207
        }
208
209
        $options = $result = null;
210
211
        if (isset($id)) {
212
213
            if ($this->getParam('entity')) {
214
                $options = array('entity' => $id);
215
            }
216
217
        } else if (!empty($all)) {
218
            $options = array();
219
        }
220
221
        if (isset($options)) {
222
223
            $deleted = $count = 0;
224
            foreach ($this->alias->getList($options) as $item) {
225
                $count++;
226
                $deleted += (int) $this->alias->delete($item['alias_id']);
227
            }
228
229
            $result = $count && $count == $deleted;
230
231
        } else if (isset($id)) {
232
233
            if (!is_numeric($id)) {
234
                $this->errorAndExit($this->text('Invalid argument'));
235
            }
236
237
            $result = $this->alias->delete($id);
238
        }
239
240
        if (empty($result)) {
241
            $this->errorAndExit($this->text('Unexpected result'));
242
        }
243
244
        $this->output();
245
    }
246
247
    /**
248
     * Returns an array of URL aliases
249
     * @return array
250
     */
251
    protected function getListAlias()
252
    {
253
        $id = $this->getParam(0);
254
255
        if (!isset($id)) {
256
            return $this->alias->getList(array('limit' => $this->getLimit()));
257
        }
258
259
        if ($this->getParam('entity')) {
260
            return $this->alias->getList(array('entity' => $id, 'limit' => $this->getLimit()));
261
        }
262
263
        if (!is_numeric($id)) {
264
            $this->errorAndExit($this->text('Invalid argument'));
265
        }
266
267
        $result = $this->alias->get($id);
268
269
        if (empty($result)) {
270
            $this->errorAndExit($this->text('Unexpected result'));
271
        }
272
273
        return array($result);
274
    }
275
276
    /**
277
     * Output table format
278
     * @param array $items
279
     */
280
    protected function outputFormatTableAlias(array $items)
281
    {
282
        $header = array(
283
            $this->text('ID'),
284
            $this->text('Alias'),
285
            $this->text('Entity'),
286
            $this->text('Entity ID')
287
        );
288
289
        $rows = array();
290
291
        foreach ($items as $item) {
292
            $rows[] = array(
293
                $item['alias_id'],
294
                $item['alias'],
295
                $item['entity'],
296
                $item['entity_id']
297
            );
298
        }
299
300
        $this->outputFormatTable($rows, $header);
301
    }
302
303
}
304