Completed
Push — master ( 6c8ffa...b951a8 )
by Iurii
01:25
created

ImageStyle   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 1
dl 0
loc 246
rs 9
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A cmdUpdateImageStyle() 0 5 1
A cmdAddImageStyle() 0 10 2
A cmdDeleteImageStyle() 0 14 3
A cmdClearCacheImageStyle() 0 21 4
A cmdGetImageStyle() 0 7 1
A getListImageStyle() 0 16 3
B outputFormatTableImageStyle() 0 23 5
A addImageStyle() 0 10 3
A updateImageStyle() 0 6 3
A submitAddImageStyle() 0 7 1
A submitUpdateImageStyle() 0 15 3
A wizardAddImageStyle() 0 10 1
A validateInputActionsImageStyle() 0 10 2
A setSubmittedActionsImageStyle() 0 8 2
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\ImageStyle as ImageStyleModel;
13
use gplcart\modules\cli\controllers\Base;
14
15
/**
16
 * Handles commands related to image styles
17
 */
18
class ImageStyle extends Base
19
{
20
21
    /**
22
     * Image style model class instance
23
     * @var \gplcart\core\models\ImageStyle $image_style
24
     */
25
    protected $image_style;
26
27
    /**
28
     * @param ImageStyleModel $image_style
29
     */
30
    public function __construct(ImageStyleModel $image_style)
31
    {
32
        parent::__construct();
33
34
        $this->image_style = $image_style;
35
    }
36
37
    /**
38
     * Callback for "imagestyle-update"
39
     * Update an image style
40
     */
41
    public function cmdUpdateImageStyle()
42
    {
43
        $this->submitUpdateImageStyle();
44
        $this->output();
45
    }
46
47
    /**
48
     * Callback for "imagestyle-add"
49
     * Adds a new image style
50
     */
51
    public function cmdAddImageStyle()
52
    {
53
        if ($this->getParam()) {
54
            $this->submitAddImageStyle();
55
        } else {
56
            $this->wizardAddImageStyle();
57
        }
58
59
        $this->output();
60
    }
61
62
    /**
63
     * Callback for "imagestyle-delete" command
64
     * Delete an image style
65
     */
66
    public function cmdDeleteImageStyle()
67
    {
68
        $id = $this->getParam(0);
69
70
        if (empty($id)) {
71
            $this->errorExit($this->text('Invalid ID'));
72
        }
73
74
        if (!$this->image_style->delete($id)) {
75
            $this->errorExit($this->text('Image style has not been deleted'));
76
        }
77
78
        $this->output();
79
    }
80
81
    /**
82
     * Callback for "imagestyle-clear-cache" command
83
     * Delete cached images
84
     */
85
    public function cmdClearCacheImageStyle()
86
    {
87
        $id = $this->getParam(0);
88
        $all = $this->getParam('all');
89
90
        $result = false;
91
92
        if (!empty($id)) {
93
            $result = $this->image_style->clearCache($id);
94
        } else if (!empty($all)) {
95
            $result = $this->image_style->clearCache();
96
        } else {
97
            $this->errorExit($this->text('Invalid command'));
98
        }
99
100
        if (!$result) {
101
            $this->errorExit($this->text('An error occurred'));
102
        }
103
104
        $this->output();
105
    }
106
107
    /**
108
     * Callback for "imagestyle-get" command
109
     * List one or a list of image styles
110
     */
111
    public function cmdGetImageStyle()
112
    {
113
        $list = $this->getListImageStyle();
114
        $this->outputFormat($list);
115
        $this->outputFormatTableImageStyle($list);
116
        $this->output();
117
    }
118
119
    /**
120
     * Returns an array of image styles
121
     * @return array
122
     */
123
    protected function getListImageStyle()
124
    {
125
        $id = $this->getParam(0);
126
127
        if (isset($id)) {
128
            $result = $this->image_style->get($id);
129
            if (empty($result)) {
130
                $this->errorExit($this->text('Invalid ID'));
131
            }
132
            return array($result);
133
        }
134
135
        $list = $this->image_style->getList();
136
        $this->limitItems($list);
137
        return $list;
138
    }
139
140
    /**
141
     * Output image styles in a table
142
     * @param array $items
143
     */
144
    protected function outputFormatTableImageStyle(array $items)
145
    {
146
        $header = array(
147
            $this->text('ID'),
148
            $this->text('Name'),
149
            $this->text('Default'),
150
            $this->text('In database'),
151
            $this->text('Enabled')
152
        );
153
154
        $rows = array();
155
        foreach ($items as $item) {
156
            $rows[] = array(
157
                $item['imagestyle_id'],
158
                $this->text($item['name']),
159
                empty($item['default']) ? $this->text('No') : $this->text('Yes'),
160
                empty($item['in_database']) ? $this->text('No') : $this->text('Yes'),
161
                empty($item['status']) ? $this->text('No') : $this->text('Yes'),
162
            );
163
        }
164
165
        $this->outputFormatTable($rows, $header);
166
    }
167
168
    /**
169
     * Adds an image style
170
     */
171
    protected function addImageStyle()
172
    {
173
        if (!$this->isError()) {
174
            $id = $this->image_style->add($this->getSubmitted());
175
            if (empty($id)) {
176
                $this->errorExit($this->text('Image style has not been added'));
177
            }
178
            $this->line($id);
179
        }
180
    }
181
182
    /**
183
     * Updates an image style
184
     * @param int $imagestyle_id
185
     */
186
    protected function updateImageStyle($imagestyle_id)
187
    {
188
        if (!$this->isError() && !$this->image_style->update($imagestyle_id, $this->getSubmitted())) {
189
            $this->errorExit($this->text('Image style has not been updated'));
190
        }
191
    }
192
193
    /**
194
     * Add an image style at once
195
     */
196
    protected function submitAddImageStyle()
197
    {
198
        $this->setSubmitted(null, $this->getParam());
199
        $this->setSubmittedActionsImageStyle();
200
        $this->validateComponent('image_style');
201
        $this->addImageStyle();
202
    }
203
204
    /**
205
     * Updates an image style
206
     */
207
    protected function submitUpdateImageStyle()
208
    {
209
        $params = $this->getParam();
210
211
        if (empty($params[0]) || count($params) < 2) {
212
            $this->errorExit($this->text('Invalid command'));
213
        }
214
215
        $this->setSubmitted(null, $params);
216
        $this->setSubmittedActionsImageStyle();
217
        $this->setSubmitted('update', $params[0]);
218
219
        $this->validateComponent('image_style');
220
        $this->updateImageStyle($params[0]);
221
    }
222
223
    /**
224
     * Adds an image style step-by-step
225
     */
226
    protected function wizardAddImageStyle()
227
    {
228
        $this->validateInput('name', $this->text('Name'), 'image_style');
229
        $this->validateInputActionsImageStyle();
230
        $this->validateInput('status', $this->text('Status'), 'image_style', 0);
231
        $this->setSubmittedActionsImageStyle();
232
233
        $this->validateComponent('image_style');
234
        $this->addImageStyle();
235
    }
236
237
    /**
238
     * Validate actions
239
     */
240
    protected function validateInputActionsImageStyle()
241
    {
242
        $input = $this->prompt($this->text('Actions'));
243
        if ($this->isValidInput($this->explodeByPipe($input), 'actions', 'image_style')) {
244
            $this->setSubmitted('actions', $input);
245
        } else {
246
            $this->errors();
247
            $this->validateInputActionsImageStyle();
248
        }
249
    }
250
251
    /**
252
     * Sets image style actions
253
     */
254
    protected function setSubmittedActionsImageStyle()
255
    {
256
        $actions = $this->getSubmitted('actions');
257
258
        if (isset($actions)) {
259
            $this->setSubmitted('actions', $this->explodeByPipe($actions));
260
        }
261
    }
262
263
}
264