Completed
Push — master ( 35e61c...496f6c )
by Iurii
01:33
created

ImageStyle::cmdClearCacheImageStyle()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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