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

File::cmdDeleteFile()   C

Complexity

Conditions 14
Paths 72

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 5.3716
c 0
b 0
f 0
cc 14
eloc 28
nc 72
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\models\File as FileModel;
13
use gplcart\modules\cli\controllers\Command;
14
15
/**
16
 * Handles commands related to database files
17
 */
18
class File extends Command
19
{
20
21
    /**
22
     * File model instance
23
     * @var \gplcart\core\models\File $file
24
     */
25
    protected $file;
26
27
    /**
28
     * @param FileModel $file
29
     */
30
    public function __construct(FileModel $file)
31
    {
32
        parent::__construct();
33
34
        $this->file = $file;
35
    }
36
37
    /**
38
     * Callback for "file-get" command
39
     */
40
    public function cmdGetFile()
41
    {
42
        $result = $this->getListFile();
43
        $this->outputFormat($result);
44
        $this->outputFormatTableFile($result);
45
        $this->output();
46
    }
47
48
    /**
49
     * Callback for "file-delete" command
50
     */
51
    public function cmdDeleteFile()
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('file_type' => $id);
66
            } else if ($this->getParam('mime')) {
67
                $options = array('mime_type' => $id);
68
            } else if ($this->getParam('entity')) {
69
                $options = array('entity' => $id);
70
            }
71
72
        } else if (!empty($all)) {
73
            $options = array();
74
        }
75
76
        if (isset($options)) {
77
78
            $deleted = $count = 0;
79
            foreach ($this->file->getList($options) as $item) {
80
                $count++;
81
                $deleted += (int) $this->deleteFile($item);
82
            }
83
84
            $result = $count && $count == $deleted;
85
86
        } else {
87
88
            if (empty($id) || !is_numeric($id)) {
89
                $this->errorAndExit($this->text('Invalid argument'));
90
            }
91
92
            $result = $this->deleteFile($id);
93
        }
94
95
        if (empty($result)) {
96
            $this->errorAndExit($this->text('Unexpected result'));
97
        }
98
99
        $this->output();
100
    }
101
102
    /**
103
     * Delete a file
104
     * @param int|array $file
105
     * @return bool
106
     */
107
    protected function deleteFile($file)
108
    {
109
        if ($this->getParam('disk')) {
110
            return array_sum($this->file->deleteAll($file)) == 2;
111
        }
112
113
        if (isset($file['file_id'])) {
114
            $file = $file['file_id'];
115
        }
116
117
        return $this->file->delete($file);
118
    }
119
120
    /**
121
     * Callback for "file-add" command
122
     */
123
    public function cmdAddFile()
124
    {
125
        if ($this->getParam()) {
126
            $this->submitAddFile();
127
        } else {
128
            $this->wizardAddFile();
129
        }
130
131
        $this->output();
132
    }
133
134
    /**
135
     * Callback for "file-update" command
136
     */
137
    public function cmdUpdateFile()
138
    {
139
        $params = $this->getParam();
140
141
        if (empty($params[0]) || count($params) < 2) {
142
            $this->errorAndExit($this->text('Invalid command'));
143
        }
144
145
        if (!is_numeric($params[0])) {
146
            $this->errorAndExit($this->text('Invalid argument'));
147
        }
148
149
        $this->setSubmitted(null, $params);
150
        $this->setSubmitted('update', $params[0]);
151
        $this->validateComponent('file');
152
153
        $this->updateFile($params[0]);
154
        $this->output();
155
    }
156
157
    /**
158
     * Returns an array of files
159
     * @return array
160
     */
161
    protected function getListFile()
162
    {
163
        $id = $this->getParam(0);
164
165
        if (!isset($id)) {
166
            return $this->file->getList(array('limit' => $this->getLimit()));
167
        }
168
169
        if ($this->getParam('type')) {
170
            return $this->file->getList(array('file_type' => $id, 'limit' => $this->getLimit()));
171
        }
172
173
        if ($this->getParam('mime')) {
174
            return $this->file->getList(array('mime_type' => $id, 'limit' => $this->getLimit()));
175
        }
176
177
        if ($this->getParam('entity')) {
178
            return $this->file->getList(array('entity' => $id, 'limit' => $this->getLimit()));
179
        }
180
181
        if (!is_numeric($id)) {
182
            $this->errorAndExit($this->text('Invalid argument'));
183
        }
184
185
        $result = $this->file->get($id);
186
187
        if (empty($result)) {
188
            $this->errorAndExit($this->text('Unexpected result'));
189
        }
190
191
        return array($result);
192
    }
193
194
    /**
195
     * Output table format
196
     * @param array $items
197
     */
198
    protected function outputFormatTableFile(array $items)
199
    {
200
        $header = array(
201
            $this->text('ID'),
202
            $this->text('Title'),
203
            $this->text('Entity'),
204
            $this->text('Entity ID'),
205
            $this->text('MIME type'),
206
            $this->text('Path')
207
        );
208
209
        $rows = array();
210
211
        foreach ($items as $item) {
212
            $rows[] = array(
213
                $item['file_id'],
214
                $item['title'],
215
                $item['entity'],
216
                $item['entity_id'],
217
                $item['mime_type'],
218
                $item['path']
219
            );
220
        }
221
222
        $this->outputFormatTable($rows, $header);
223
    }
224
225
    /**
226
     * Add a new file record
227
     */
228
    protected function addFile()
229
    {
230
        if (!$this->isError()) {
231
            $id = $this->file->add($this->getSubmitted());
232
            if (empty($id)) {
233
                $this->errorAndExit($this->text('Unexpected result'));
234
            }
235
            $this->line($id);
236
        }
237
    }
238
239
    /**
240
     * Updates a file record
241
     * @param string $file_id
242
     */
243
    protected function updateFile($file_id)
244
    {
245
        if (!$this->isError() && !$this->file->update($file_id, $this->getSubmitted())) {
246
            $this->errorAndExit($this->text('Unexpected result'));
247
        }
248
    }
249
250
    /**
251
     * Add a new file record at once
252
     */
253
    protected function submitAddFile()
254
    {
255
        $this->setSubmitted(null, $this->getParam());
256
        $this->validateComponent('file');
257
        $this->addFile();
258
    }
259
260
    /**
261
     * Add a new file record step by step
262
     */
263
    protected function wizardAddFile()
264
    {
265
        $this->validatePrompt('path', $this->text('Path'), 'file');
266
        $this->validatePrompt('entity', $this->text('Entity'), 'file');
267
        $this->validatePrompt('entity_id', $this->text('Entity ID'), 'file', 0);
268
        $this->validatePrompt('title', $this->text('Title'), 'file', '');
269
        $this->validatePrompt('description', $this->text('Description'), 'file', '');
270
        $this->validatePrompt('weight', $this->text('Weight'), 'file', 0);
271
272
        $this->validateComponent('file');
273
        $this->addFile();
274
    }
275
276
}
277