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

Country::outputFormatTableCountry()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 16
nc 3
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\Country as CountryModel;
13
14
/**
15
 * Handles commands related to countries
16
 */
17
class Country extends Base
18
{
19
20
    /**
21
     * Country model instance
22
     * @var \gplcart\core\models\Country $country
23
     */
24
    protected $country;
25
26
    /**
27
     * @param CountryModel $country
28
     */
29
    public function __construct(CountryModel $country)
30
    {
31
        parent::__construct();
32
33
        $this->country = $country;
34
    }
35
36
    /**
37
     * Callback for "country-get" command
38
     */
39
    public function cmdGetCountry()
40
    {
41
        $result = $this->getListCountry();
42
        $this->outputFormat($result);
43
        $this->outputFormatTableCountry($result);
44
        $this->output();
45
    }
46
47
    /**
48
     * Callback for "country-add" command
49
     */
50
    public function cmdAddCountry()
51
    {
52
        if ($this->getParam()) {
53
            $this->submitAddCountry();
54
        } else {
55
            $this->wizardAddCountry();
56
        }
57
58
        $this->output();
59
    }
60
61
    /**
62
     * Callback for "country-update" command
63
     */
64
    public function cmdUpdateCountry()
65
    {
66
        $params = $this->getParam();
67
68
        if (empty($params[0]) || count($params) < 2) {
69
            $this->errorExit($this->text('Invalid command'));
70
        }
71
72
        $this->setSubmitted(null, $this->getParam());
73
        $this->setSubmitted('update', $params[0]);
74
        $this->setSubmittedJson('format');
75
        $this->validateComponent('country');
76
        $this->updateCountry($params[0]);
77
        $this->output();
78
    }
79
80
    /**
81
     * Callback for "country-delete" command
82
     */
83
    public function cmdDeleteCountry()
84
    {
85
        $code = $this->getParam(0);
86
87
        if (empty($code)) {
88
            $this->errorExit($this->text('Invalid ID'));
89
        }
90
91
        if (!$this->country->delete($code)) {
92
            $this->errorExit($this->text('Country has not been deleted'));
93
        }
94
95
        $this->output();
96
    }
97
98
    /**
99
     * Returns an array of countries
100
     * @return array
101
     */
102
    protected function getListCountry()
103
    {
104
        $code = $this->getParam(0);
105
106
        if (!isset($code)) {
107
            return $this->country->getList(array('limit' => $this->getLimit()));
108
        }
109
110
        $country = $this->country->get($code);
111
112
        if (empty($country)) {
113
            $this->errorExit($this->text('Invalid ID'));
114
        }
115
116
        return array($country);
117
    }
118
119
    /**
120
     * Output table format
121
     * @param array $items
122
     */
123
    protected function outputFormatTableCountry(array $items)
124
    {
125
        $header = array(
126
            $this->text('Code'),
127
            $this->text('Name'),
128
            $this->text('Native name'),
129
            $this->text('Weight'),
130
            $this->text('Enabled')
131
        );
132
133
        $rows = array();
134
135
        foreach ($items as $item) {
136
            $rows[] = array(
137
                $item['code'],
138
                $this->truncate($item['name'], 50),
139
                $this->truncate($item['native_name'], 50),
140
                $item['weight'],
141
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
142
            );
143
        }
144
145
        $this->outputFormatTable($rows, $header);
146
    }
147
148
    /**
149
     * Add a new country
150
     */
151
    protected function addCountry()
152
    {
153
        if (!$this->isError() && !$this->country->add($this->getSubmitted())) {
154
            $this->errorExit($this->text('Country has not been added'));
155
        }
156
    }
157
158
    /**
159
     * Updates an country
160
     * @param string $code
161
     */
162
    protected function updateCountry($code)
163
    {
164
        if (!$this->isError() && !$this->country->update($code, $this->getSubmitted())) {
165
            $this->errorExit($this->text('Country has not been updated'));
166
        }
167
    }
168
169
    /**
170
     * Add a new country at once
171
     */
172
    protected function submitAddCountry()
173
    {
174
        $this->setSubmitted(null, $this->getParam());
175
        $this->setSubmittedJson('format');
176
        $this->validateComponent('country');
177
        $this->addCountry();
178
    }
179
180
    /**
181
     * Add a new country step by step
182
     */
183
    protected function wizardAddCountry()
184
    {
185
        $this->validatePrompt('code', $this->text('Code'), 'country');
186
        $this->validatePrompt('name', $this->text('Name'), 'country', '');
187
        $this->validatePrompt('native_name', $this->text('Native name'), 'country', '');
188
        $this->validatePrompt('zone_id', $this->text('Zone'), 'country', 0);
189
        $this->validatePrompt('status', $this->text('Status'), 'country', 0);
190
        $this->validatePrompt('weight', $this->text('Weight'), 'country', 0);
191
        $this->validateComponent('country');
192
        $this->addCountry();
193
    }
194
195
}
196