Completed
Push — master ( 496f6c...fa7820 )
by Iurii
01:48
created

Address::cmdDeleteAddress()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
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\Address as AddressModel;
13
14
/**
15
 * Handles commands related to user addresses
16
 */
17
class Address extends Base
18
{
19
20
    /**
21
     * Address model instance
22
     * @var \gplcart\core\models\Address $address
23
     */
24
    protected $address;
25
26
    /**
27
     * @param AddressModel $alias
28
     */
29
    public function __construct(AddressModel $alias)
30
    {
31
        parent::__construct();
32
33
        $this->address = $alias;
34
    }
35
36
    /**
37
     * Callback for "address-get" command
38
     */
39
    public function cmdGetAddress()
40
    {
41
        $result = $this->getListAddress();
42
        $this->outputFormat($result);
43
        $this->outputFormatTableAddress($result);
44
        $this->output();
45
    }
46
47
    /**
48
     * Callback for "address-delete" command
49
     */
50
    public function cmdDeleteAddress()
51
    {
52
        $id = $this->getParam(0);
53
54
        if (empty($id)) {
55
            $this->errorAndExit($this->text('Invalid argument'));
56
        }
57
58
        if ($this->getParam('user')) {
59
60
            $deleted = $count = 0;
61
            foreach ($this->address->getList(array('user_id' => $id)) as $item) {
62
                $count++;
63
                $deleted += (int) $this->address->delete($item['address_id']);
64
            }
65
66
            $result = $count && $count == $deleted;
67
68
        } else {
69
70
            if (!is_numeric($id)) {
71
                $this->errorAndExit($this->text('Invalid argument'));
72
            }
73
74
            $result = $this->address->delete($id);
75
        }
76
77
        if (empty($result)) {
78
            $this->errorAndExit($this->text('Unexpected result'));
79
        }
80
81
        $this->output();
82
    }
83
84
    /**
85
     * Callback for "address-add" command
86
     */
87
    public function cmdAddAddress()
88
    {
89
        if ($this->getParam()) {
90
            $this->submitAddAddress();
91
        } else {
92
            $this->wizardAddAddress();
93
        }
94
95
        $this->output();
96
    }
97
98
    /**
99
     * Callback for "address-update" command
100
     */
101
    public function cmdUpdateAddress()
102
    {
103
        $params = $this->getParam();
104
105
        if (empty($params[0]) || count($params) < 2) {
106
            $this->errorAndExit($this->text('Invalid command'));
107
        }
108
109
        if (!is_numeric($params[0])) {
110
            $this->errorAndExit($this->text('Invalid argument'));
111
        }
112
113
        $this->setSubmitted(null, $params);
114
        $this->setSubmitted('update', $params[0]);
115
        $this->setSubmittedJson('data');
116
        $this->validateComponent('address');
117
        $this->updateAddress($params[0]);
118
119
        $this->output();
120
    }
121
122
    /**
123
     * Returns an array of addresses
124
     * @return array
125
     */
126
    protected function getListAddress()
127
    {
128
        $id = $this->getParam(0);
129
130
        if (!isset($id)) {
131
            return $this->address->getList(array('limit' => $this->getLimit()));
132
        }
133
134
        if ($this->getParam('user')) {
135
            return $this->address->getList(array('user_id' => $id, 'limit' => $this->getLimit()));
136
        }
137
138
        if (!is_numeric($id)) {
139
            $this->errorAndExit($this->text('Invalid argument'));
140
        }
141
142
        $result = $this->address->get($id);
143
144
        if (empty($result)) {
145
            $this->errorAndExit($this->text('Unexpected result'));
146
        }
147
148
        return array($result);
149
    }
150
151
    /**
152
     * Output table format
153
     * @param array $items
154
     */
155
    protected function outputFormatTableAddress(array $items)
156
    {
157
        $header = array(
158
            $this->text('ID'),
159
            $this->text('User ID'),
160
            $this->text('Full name'),
161
            $this->text('Address'),
162
            $this->text('City ID'),
163
            $this->text('Phone')
164
        );
165
166
        $rows = array();
167
        foreach ($items as $item) {
168
            $rows[] = array(
169
                $item['address_id'],
170
                $item['user_id'],
171
                $item['full_name'],
172
                $item['address_id'],
173
                $item['city_id'],
174
                $item['phone'],
175
            );
176
        }
177
178
        $this->outputFormatTable($rows, $header);
179
    }
180
181
    /**
182
     * Add a new address
183
     */
184
    protected function addAddress()
185
    {
186
        if (!$this->isError()) {
187
188
            $id = $this->address->add($this->getSubmitted());
189
190
            if (empty($id)) {
191
                $this->errorAndExit($this->text('Unexpected result'));
192
            }
193
194
            $this->line($id);
195
        }
196
    }
197
198
    /**
199
     * Updates an address
200
     * @param string $address_id
201
     */
202
    protected function updateAddress($address_id)
203
    {
204
        if (!$this->isError() && !$this->address->update($address_id, $this->getSubmitted())) {
205
            $this->errorAndExit($this->text('Unexpected result'));
206
        }
207
    }
208
209
    /**
210
     * Add a new address at once
211
     */
212
    protected function submitAddAddress()
213
    {
214
        $this->setSubmitted(null, $this->getParam());
215
        $this->setSubmittedJson('data');
216
        $this->validateComponent('address');
217
        $this->addAddress();
218
    }
219
220
    /**
221
     * Add a new address step by step
222
     */
223
    protected function wizardAddAddress()
224
    {
225
        $types = $this->address->getTypes();
226
        $this->validateMenu('type', $this->text('Address type'), 'address', array_combine($types, $types), 'shipping');
227
228
        $this->validatePrompt('user_id', $this->text('User ID'), 'address');
229
        $this->validatePrompt('country', $this->text('Country code'), 'address');
230
        $this->validatePrompt('state_id', $this->text('State ID'), 'address');
231
        $this->validatePrompt('city_id', $this->text('City ID'), 'address');
232
        $this->validatePrompt('address_1', $this->text('Address'), 'address');
233
        $this->validatePrompt('address_2', $this->text('Additional address'), 'address');
234
        $this->validatePrompt('phone', $this->text('Phone'), 'address');
235
        $this->validatePrompt('postcode', $this->text('Post code/ZIP'), 'address');
236
        $this->validatePrompt('company', $this->text('Company'), 'address');
237
        $this->validatePrompt('fax', $this->text('Fax'), 'address');
238
        $this->validatePrompt('first_name', $this->text('First name'), 'address');
239
        $this->validatePrompt('middle_name', $this->text('Middle name'), 'address');
240
        $this->validatePrompt('last_name', $this->text('Last name'), 'address');
241
        $this->validatePrompt('data', $this->text('Data'), 'address');
242
243
        $this->setSubmittedJson('data');
244
        $this->validateComponent('address');
245
        $this->addAddress();
246
    }
247
248
}
249