Completed
Push — master ( 1cf59b...5e1af1 )
by Iurii
01:45
created

File::cmdAddFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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