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

State::__construct()   A

Complexity

Conditions 1
Paths 1

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