Completed
Push — master ( 311420...74254e )
by Iurii
01:12
created

Zone   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 1
dl 0
loc 195
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A cmdGetZone() 0 7 1
A cmdAddZone() 0 10 2
A cmdUpdateZone() 0 5 1
C cmdDeleteZone() 0 28 7
A getListZone() 0 16 3
A outputFormatTableZone() 0 19 3
A submitUpdateZone() 0 13 3
A updateZone() 0 6 3
A submitAddZone() 0 6 1
A addZone() 0 10 3
A wizardAddZone() 0 7 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\Zone as ZoneModel;
13
use gplcart\modules\cli\controllers\Base;
14
15
/**
16
 * Handles commands related to zones
17
 */
18
class Zone extends Base
19
{
20
21
    /**
22
     * Zone model instance
23
     * @var \gplcart\core\models\Zone $zone
24
     */
25
    protected $zone;
26
27
    /**
28
     * @param ZoneModel $zone
29
     */
30
    public function __construct(ZoneModel $zone)
31
    {
32
        parent::__construct();
33
34
        $this->zone = $zone;
35
    }
36
37
    /**
38
     * Callback for "zone-get" command
39
     */
40
    public function cmdGetZone()
41
    {
42
        $result = $this->getListZone();
43
        $this->outputFormat($result);
44
        $this->outputFormatTableZone($result);
45
        $this->output();
46
    }
47
48
    /**
49
     * Callback for "zone-add" command
50
     * Add a new zone
51
     */
52
    public function cmdAddZone()
53
    {
54
        if ($this->getParam()) {
55
            $this->submitAddZone();
56
        } else {
57
            $this->wizardAddZone();
58
        }
59
60
        $this->output();
61
    }
62
63
    /**
64
     * Callback for "zone-update" command
65
     */
66
    public function cmdUpdateZone()
67
    {
68
        $this->submitUpdateZone();
69
        $this->output();
70
    }
71
72
    /**
73
     * Callback for "zone-delete" command
74
     */
75
    public function cmdDeleteZone()
76
    {
77
        $id = $this->getParam(0);
78
        $all = $this->getParam('all');
79
80
        if (empty($id) && empty($all)) {
81
            $this->errorExit($this->text('Invalid command'));
82
        }
83
84
        $result = false;
85
86
        if (!empty($id)) {
87
            $result = $this->zone->delete($id);
88
        } else if (!empty($all)) {
89
            $deleted = $count = 0;
90
            foreach ($this->zone->getList() as $zone) {
91
                $count++;
92
                $deleted += (int) $this->zone->delete($zone['zone_id']);
93
            }
94
            $result = ($count == $deleted);
95
        }
96
97
        if (!$result) {
98
            $this->errorExit($this->text('An error occurred'));
99
        }
100
101
        $this->output();
102
    }
103
104
    /**
105
     * Returns an array of zones
106
     * @return array
107
     */
108
    protected function getListZone()
109
    {
110
        $code = $this->getParam(0);
111
112
        if (isset($code)) {
113
            $zone = $this->zone->get($code);
114
            if (empty($zone)) {
115
                $this->errorExit($this->text('Invalid ID'));
116
            }
117
            return array($zone);
118
        }
119
120
        $list = $this->zone->getList();
121
        $this->limitItems($list);
122
        return $list;
123
    }
124
125
    /**
126
     * Output table format
127
     * @param array $items
128
     */
129
    protected function outputFormatTableZone(array $items)
130
    {
131
        $header = array(
132
            $this->text('ID'),
133
            $this->text('Title'),
134
            $this->text('Enabled')
135
        );
136
137
        $rows = array();
138
        foreach ($items as $item) {
139
            $rows[] = array(
140
                $item['zone_id'],
141
                $item['title'],
142
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
143
            );
144
        }
145
146
        $this->outputFormatTable($rows, $header);
147
    }
148
149
    /**
150
     * Updates a zone
151
     */
152
    protected function submitUpdateZone()
153
    {
154
        $params = $this->getParam();
155
        if (empty($params[0]) || count($params) < 2) {
156
            $this->errorExit($this->text('Invalid command'));
157
        }
158
159
        $this->setSubmitted(null, $this->getParam());
160
        $this->setSubmitted('update', $params[0]);
161
162
        $this->validateComponent('zone');
163
        $this->updateZone($params[0]);
164
    }
165
166
    /**
167
     * Updates a zone
168
     * @param string $zone_id
169
     */
170
    protected function updateZone($zone_id)
171
    {
172
        if (!$this->isError() && !$this->zone->update($zone_id, $this->getSubmitted())) {
173
            $this->errorExit($this->text('Zone has not been updated'));
174
        }
175
    }
176
177
    /**
178
     * Add a new zone at once
179
     */
180
    protected function submitAddZone()
181
    {
182
        $this->setSubmitted(null, $this->getParam());
183
        $this->validateComponent('zone');
184
        $this->addZone();
185
    }
186
187
    /**
188
     * Add a new zone
189
     */
190
    protected function addZone()
191
    {
192
        if (!$this->isError()) {
193
            $id = $this->zone->add($this->getSubmitted());
194
            if (empty($id)) {
195
                $this->errorExit($this->text('Zone has not been added'));
196
            }
197
            $this->line($id);
198
        }
199
    }
200
201
    /**
202
     * Add a new zone step by step
203
     */
204
    protected function wizardAddZone()
205
    {
206
        $this->validateInput('title', $this->text('Title'), 'zone');
207
        $this->validateInput('status', $this->text('Status'), 'zone', 0);
208
        $this->validateComponent('zone');
209
        $this->addZone();
210
    }
211
212
}
213