Completed
Push — master ( 853dbc...35e61c )
by Iurii
01:26
created

ImageStyle::cmdClearCacheImageStyle()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 13
nc 12
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->errorExit($this->text('Invalid command'));
45
        }
46
47
        if (!is_numeric($params[0])) {
48
            $this->errorExit($this->text('Invalid ID'));
49
        }
50
51
        $this->setSubmitted(null, $params);
52
        $this->setSubmittedList('actions');
53
        $this->setSubmitted('update', $params[0]);
54
        $this->validateComponent('image_style');
55
        $this->updateImageStyle($params[0]);
56
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->errorExit($this->text('Invalid ID'));
83
        }
84
85
        if (!$this->image_style->delete($id)) {
86
            $this->errorExit($this->text('Image style has not been deleted'));
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->errorExit($this->text('Invalid command'));
102
        }
103
104
        $result = false;
105
106
        if (isset($id)) {
107
            $result = $this->image_style->clearCache($id);
108
        } else if (!empty($all)) {
109
            $result = $this->image_style->clearCache();
110
        }
111
112
        if (!$result) {
113
            $this->errorExit($this->text('An error occurred'));
114
        }
115
116
        $this->output();
117
    }
118
119
    /**
120
     * Callback for "imagestyle-get" command
121
     */
122
    public function cmdGetImageStyle()
123
    {
124
        $list = $this->getListImageStyle();
125
        $this->outputFormat($list);
126
        $this->outputFormatTableImageStyle($list);
127
        $this->output();
128
    }
129
130
    /**
131
     * Returns an array of image styles
132
     * @return array
133
     */
134
    protected function getListImageStyle()
135
    {
136
        $id = $this->getParam(0);
137
138
        if (!isset($id)) {
139
            $list = $this->image_style->getList();
140
            $this->limitArray($list);
141
            return $list;
142
        }
143
144
        if (!is_numeric($id)) {
145
            $this->errorExit($this->text('Invalid ID'));
146
        }
147
148
        $result = $this->image_style->get($id);
149
150
        if (empty($result)) {
151
            $this->errorExit($this->text('Invalid ID'));
152
        }
153
154
        return array($result);
155
    }
156
157
    /**
158
     * Output image styles in a table
159
     * @param array $items
160
     */
161
    protected function outputFormatTableImageStyle(array $items)
162
    {
163
        $header = array(
164
            $this->text('ID'),
165
            $this->text('Name'),
166
            $this->text('Default'),
167
            $this->text('In database'),
168
            $this->text('Enabled')
169
        );
170
171
        $rows = array();
172
173
        foreach ($items as $item) {
174
            $rows[] = array(
175
                $item['imagestyle_id'],
176
                $this->text($item['name']),
177
                empty($item['default']) ? $this->text('No') : $this->text('Yes'),
178
                empty($item['in_database']) ? $this->text('No') : $this->text('Yes'),
179
                empty($item['status']) ? $this->text('No') : $this->text('Yes'),
180
            );
181
        }
182
183
        $this->outputFormatTable($rows, $header);
184
    }
185
186
    /**
187
     * Adds an image style
188
     */
189
    protected function addImageStyle()
190
    {
191
        if (!$this->isError()) {
192
            $id = $this->image_style->add($this->getSubmitted());
193
            if (empty($id)) {
194
                $this->errorExit($this->text('Image style has not been added'));
195
            }
196
            $this->line($id);
197
        }
198
    }
199
200
    /**
201
     * Updates an image style
202
     * @param int $imagestyle_id
203
     */
204
    protected function updateImageStyle($imagestyle_id)
205
    {
206
        if (!$this->isError() && !$this->image_style->update($imagestyle_id, $this->getSubmitted())) {
207
            $this->errorExit($this->text('Image style has not been updated'));
208
        }
209
    }
210
211
    /**
212
     * Add an image style at once
213
     */
214
    protected function submitAddImageStyle()
215
    {
216
        $this->setSubmitted(null, $this->getParam());
217
        $this->setSubmittedList('actions');
218
        $this->validateComponent('image_style');
219
        $this->addImageStyle();
220
    }
221
222
    /**
223
     * Adds an image style step-by-step
224
     */
225
    protected function wizardAddImageStyle()
226
    {
227
        $this->validatePrompt('name', $this->text('Name'), 'image_style');
228
        $this->validatePromptList('actions', $this->text('Actions'), 'image_style');
229
        $this->validatePrompt('status', $this->text('Status'), 'image_style', 0);
230
        $this->setSubmittedList('actions');
231
        $this->validateComponent('image_style');
232
        $this->addImageStyle();
233
    }
234
235
}
236