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

Address::cmdUpdateAddress()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
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 $role
28
     */
29
    public function __construct(AddressModel $role)
30
    {
31
        parent::__construct();
32
33
        $this->address = $role;
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->errorExit($this->text('Invalid ID'));
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 == $deleted);
67
68
        } else {
69
            $result = $this->address->delete($id);
70
        }
71
72
        if (!$result) {
73
            $this->errorExit($this->text('An error occurred'));
74
        }
75
76
        $this->output();
77
    }
78
79
    /**
80
     * Callback for "address-add" command
81
     */
82
    public function cmdAddAddress()
83
    {
84
        if ($this->getParam()) {
85
            $this->submitAddAddress();
86
        } else {
87
            $this->wizardAddAddress();
88
        }
89
90
        $this->output();
91
    }
92
93
    /**
94
     * Callback for "address-update" command
95
     */
96
    public function cmdUpdateAddress()
97
    {
98
        $params = $this->getParam();
99
100
        if (empty($params[0]) || count($params) < 2) {
101
            $this->errorExit($this->text('Invalid command'));
102
        }
103
104
        $this->setSubmitted(null, $this->getParam());
105
        $this->setSubmitted('update', $params[0]);
106
        $this->setSubmittedJson('data');
107
        $this->validateComponent('address');
108
        $this->updateAddress($params[0]);
109
110
        $this->output();
111
    }
112
113
    /**
114
     * Returns an array of addresses
115
     * @return array
116
     */
117
    protected function getListAddress()
118
    {
119
        $id = $this->getParam(0);
120
121
        if (!isset($id)) {
122
            return $this->address->getList(array('limit' => $this->getLimit()));
123
        }
124
125
        if ($this->getParam('user')) {
126
            return $this->address->getList(array('user_id' => $id, 'limit' => $this->getLimit()));
127
        }
128
129
        $result = $this->address->get($id);
130
131
        if (empty($result)) {
132
            $this->errorExit($this->text('Invalid ID'));
133
        }
134
135
        return array($result);
136
    }
137
138
    /**
139
     * Output table format
140
     * @param array $items
141
     */
142
    protected function outputFormatTableAddress(array $items)
143
    {
144
        $header = array(
145
            $this->text('ID'),
146
            $this->text('User ID'),
147
            $this->text('Full name'),
148
            $this->text('Address'),
149
            $this->text('City ID'),
150
            $this->text('Phone')
151
        );
152
153
        $rows = array();
154
        foreach ($items as $item) {
155
            $rows[] = array(
156
                $item['address_id'],
157
                $item['user_id'],
158
                $item['full_name'],
159
                $item['address_id'],
160
                $item['city_id'],
161
                $item['phone'],
162
            );
163
        }
164
165
        $this->outputFormatTable($rows, $header);
166
    }
167
168
    /**
169
     * Add a new address
170
     */
171
    protected function addAddress()
172
    {
173
        if (!$this->isError()) {
174
            $id = $this->address->add($this->getSubmitted());
175
            if (empty($id)) {
176
                $this->errorExit($this->text('Address has not been added'));
177
            }
178
            $this->line($id);
179
        }
180
    }
181
182
    /**
183
     * Updates an address
184
     * @param string $address_id
185
     */
186
    protected function updateAddress($address_id)
187
    {
188
        if (!$this->isError() && !$this->address->update($address_id, $this->getSubmitted())) {
189
            $this->errorExit($this->text('Address has not been updated'));
190
        }
191
    }
192
193
    /**
194
     * Add a new address at once
195
     */
196
    protected function submitAddAddress()
197
    {
198
        $this->setSubmitted(null, $this->getParam());
199
        $this->setSubmittedJson('data');
200
        $this->validateComponent('address');
201
        $this->addAddress();
202
    }
203
204
    /**
205
     * Add a new address step by step
206
     */
207
    protected function wizardAddAddress()
208
    {
209
        $types = $this->address->getTypes();
210
        $this->validateMenu('type', $this->text('Address type'), 'address', array_combine($types, $types), 'shipping');
211
212
        $this->validatePrompt('user_id', $this->text('User ID'), 'address');
213
        $this->validatePrompt('country', $this->text('Country code'), 'address');
214
        $this->validatePrompt('state_id', $this->text('State ID'), 'address');
215
        $this->validatePrompt('city_id', $this->text('City ID'), 'address');
216
        $this->validatePrompt('address_1', $this->text('Address'), 'address');
217
        $this->validatePrompt('address_2', $this->text('Additional address'), 'address');
218
        $this->validatePrompt('phone', $this->text('Phone'), 'address');
219
        $this->validatePrompt('postcode', $this->text('Post code/ZIP'), 'address');
220
        $this->validatePrompt('company', $this->text('Company'), 'address');
221
        $this->validatePrompt('fax', $this->text('Fax'), 'address');
222
        $this->validatePrompt('first_name', $this->text('First name'), 'address');
223
        $this->validatePrompt('middle_name', $this->text('Middle name'), 'address');
224
        $this->validatePrompt('last_name', $this->text('Last name'), 'address');
225
        $this->validatePrompt('data', $this->text('Data'), 'address');
226
227
        $this->setSubmittedJson('data');
228
        $this->validateComponent('address');
229
        $this->addAddress();
230
    }
231
232
}
233