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

Zone::submitUpdateZone()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
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\Zone as ZoneModel;
13
14
/**
15
 * Handles commands related to zones
16
 */
17
class Zone extends Base
18
{
19
20
    /**
21
     * Zone model instance
22
     * @var \gplcart\core\models\Zone $zone
23
     */
24
    protected $zone;
25
26
    /**
27
     * @param ZoneModel $zone
28
     */
29
    public function __construct(ZoneModel $zone)
30
    {
31
        parent::__construct();
32
33
        $this->zone = $zone;
34
    }
35
36
    /**
37
     * Callback for "zone-get" command
38
     */
39
    public function cmdGetZone()
40
    {
41
        $result = $this->getListZone();
42
        $this->outputFormat($result);
43
        $this->outputFormatTableZone($result);
44
        $this->output();
45
    }
46
47
    /**
48
     * Callback for "zone-add" command
49
     * Add a new zone
50
     */
51
    public function cmdAddZone()
52
    {
53
        if ($this->getParam()) {
54
            $this->submitAddZone();
55
        } else {
56
            $this->wizardAddZone();
57
        }
58
59
        $this->output();
60
    }
61
62
    /**
63
     * Callback for "zone-update" command
64
     */
65
    public function cmdUpdateZone()
66
    {
67
        $params = $this->getParam();
68
69
        if (empty($params[0]) || count($params) < 2) {
70
            $this->errorAndExit($this->text('Invalid command'));
71
        }
72
73
        if (!is_numeric($params[0])) {
74
            $this->errorAndExit($this->text('Invalid ID'));
75
        }
76
77
        $this->setSubmitted(null, $params);
78
        $this->setSubmitted('update', $params[0]);
79
80
        $this->validateComponent('zone');
81
82
        $this->updateZone($params[0]);
83
        $this->output();
84
    }
85
86
    /**
87
     * Callback for "zone-delete" command
88
     */
89
    public function cmdDeleteZone()
90
    {
91
        $id = $this->getParam(0);
92
        $all = $this->getParam('all');
93
94
        if (empty($id) && empty($all)) {
95
            $this->errorAndExit($this->text('Invalid command'));
96
        }
97
98
        $result = false;
99
100
        if (!empty($id)) {
101
            $result = $this->zone->delete($id);
102
        } else if (!empty($all)) {
103
            $deleted = $count = 0;
104
            foreach ($this->zone->getList() as $zone) {
105
                $count++;
106
                $deleted += (int) $this->zone->delete($zone['zone_id']);
107
            }
108
            $result = ($count == $deleted);
109
        }
110
111
        if (!$result) {
112
            $this->errorAndExit($this->text('An error occurred'));
113
        }
114
115
        $this->output();
116
    }
117
118
    /**
119
     * Returns an array of zones
120
     * @return array
121
     */
122
    protected function getListZone()
123
    {
124
        $id = $this->getParam(0);
125
126
        if (!isset($id)) {
127
            return $this->zone->getList(array('limit' => $this->getLimit()));
128
        }
129
130
        if (!is_numeric($id)) {
131
            $this->errorAndExit($this->text('Invalid ID'));
132
        }
133
134
        $zone = $this->zone->get($id);
135
136
        if (empty($zone)) {
137
            $this->errorAndExit($this->text('Invalid ID'));
138
        }
139
140
        return array($zone);
141
    }
142
143
    /**
144
     * Output table format
145
     * @param array $items
146
     */
147
    protected function outputFormatTableZone(array $items)
148
    {
149
        $header = array(
150
            $this->text('ID'),
151
            $this->text('Title'),
152
            $this->text('Enabled')
153
        );
154
155
        $rows = array();
156
157
        foreach ($items as $item) {
158
            $rows[] = array(
159
                $item['zone_id'],
160
                $item['title'],
161
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
162
            );
163
        }
164
165
        $this->outputFormatTable($rows, $header);
166
    }
167
168
    /**
169
     * Updates a zone
170
     * @param string $zone_id
171
     */
172
    protected function updateZone($zone_id)
173
    {
174
        if (!$this->isError() && !$this->zone->update($zone_id, $this->getSubmitted())) {
175
            $this->errorAndExit($this->text('An error occurred'));
176
        }
177
    }
178
179
    /**
180
     * Add a new zone at once
181
     */
182
    protected function submitAddZone()
183
    {
184
        $this->setSubmitted(null, $this->getParam());
185
        $this->validateComponent('zone');
186
        $this->addZone();
187
    }
188
189
    /**
190
     * Add a new zone
191
     */
192
    protected function addZone()
193
    {
194
        if (!$this->isError()) {
195
            $id = $this->zone->add($this->getSubmitted());
196
            if (empty($id)) {
197
                $this->errorAndExit($this->text('An error occurred'));
198
            }
199
            $this->line($id);
200
        }
201
    }
202
203
    /**
204
     * Add a new zone step by step
205
     */
206
    protected function wizardAddZone()
207
    {
208
        $this->validatePrompt('title', $this->text('Title'), 'zone');
209
        $this->validatePrompt('status', $this->text('Status'), 'zone', 0);
210
211
        $this->validateComponent('zone');
212
        $this->addZone();
213
    }
214
215
}
216