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

City::updateCity()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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