Completed
Push — master ( fa7820...1cf59b )
by Iurii
08:39
created

CollectionItem::updateCollectionItem()   A

Complexity

Conditions 3
Paths 2

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