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

Currency::cmdAddCurrency()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
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\Currency as CurrencyModel;
13
14
/**
15
 * Handles commands related to currencies
16
 */
17
class Currency extends Base
18
{
19
20
    /**
21
     * Currency model class instance
22
     * @var \gplcart\core\models\Currency $currency
23
     */
24
    protected $currency;
25
26
    /**
27
     * @param CurrencyModel $currency
28
     */
29
    public function __construct(CurrencyModel $currency)
30
    {
31
        parent::__construct();
32
33
        $this->currency = $currency;
34
    }
35
36
    /**
37
     * Callback for "currency-update"
38
     */
39
    public function cmdUpdateCurrency()
40
    {
41
        $params = $this->getParam();
42
43
        if (empty($params[0]) || count($params) < 2) {
44
            $this->errorExit($this->text('Invalid command'));
45
        }
46
47
        $this->setSubmitted(null, $params);
48
        $this->setSubmitted('update', $params[0]);
49
        $this->validateComponent('currency');
50
        $this->updateCurrency($params[0]);
51
        $this->output();
52
    }
53
54
    /**
55
     * Callback for "currency-add"
56
     */
57
    public function cmdAddCurrency()
58
    {
59
        if ($this->getParam()) {
60
            $this->submitAddCurrency();
61
        } else {
62
            $this->wizardAddCurrency();
63
        }
64
65
        $this->output();
66
    }
67
68
    /**
69
     * Callback for "currency-get" command
70
     */
71
    public function cmdGetCurrency()
72
    {
73
        $list = $this->getListCurrency();
74
        $this->outputFormat($list);
75
        $this->outputFormatTableCurrency($list);
76
        $this->output();
77
    }
78
79
    /**
80
     * Callback for "currency-delete" command
81
     */
82
    public function cmdDeleteCurrency()
83
    {
84
        $id = $this->getParam(0);
85
        $all = $this->getParam('all');
86
87
        if (empty($id) && empty($all)) {
88
            $this->errorExit($this->text('Invalid command'));
89
        }
90
91
        $result = false;
92
93
        if (!empty($id)) {
94
            $result = $this->currency->delete($id);
95
        } else if (!empty($all)) {
96
            $deleted = $count = 0;
97
            foreach ($this->currency->getList(array('in_database' => true)) as $currency) {
98
                $count++;
99
                $deleted += (int) $this->currency->delete($currency['code']);
100
            }
101
            $result = ($count == $deleted);
102
        }
103
104
        if (!$result) {
105
            $this->errorExit($this->text('An error occurred'));
106
        }
107
108
        $this->output();
109
    }
110
111
    /**
112
     * Returns an array of currencies
113
     * @return array
114
     */
115
    protected function getListCurrency()
116
    {
117
        $id = $this->getParam(0);
118
119
        if (!isset($id)) {
120
            $list = $this->currency->getList();
121
            $this->limitArray($list);
122
            return $list;
123
        }
124
125
        $result = $this->currency->get($id);
126
127
        if (empty($result)) {
128
            $this->errorExit($this->text('Invalid ID'));
129
        }
130
131
        return array($result);
132
    }
133
134
    /**
135
     * Output formatted table
136
     * @param array $items
137
     */
138
    protected function outputFormatTableCurrency(array $items)
139
    {
140
        $header = array(
141
            $this->text('Code'),
142
            $this->text('Name'),
143
            $this->text('Symbol'),
144
            $this->text('Conversion rate'),
145
            $this->text('In database'),
146
            $this->text('Enabled')
147
        );
148
149
        $rows = array();
150
151
        foreach ($items as $item) {
152
            $rows[] = array(
153
                $item['code'],
154
                $this->text($item['name']),
155
                $item['symbol'],
156
                $item['conversion_rate'],
157
                empty($item['in_database']) ? $this->text('No') : $this->text('Yes'),
158
                empty($item['status']) ? $this->text('No') : $this->text('Yes'),
159
            );
160
        }
161
162
        $this->outputFormatTable($rows, $header);
163
    }
164
165
    /**
166
     * Adds a currency
167
     */
168
    protected function addCurrency()
169
    {
170
        if (!$this->isError() && !$this->currency->add($this->getSubmitted())) {
171
            $this->errorExit($this->text('Currency has not been added'));
172
        }
173
    }
174
175
    /**
176
     * Updates a currency
177
     * @param string $code
178
     */
179
    protected function updateCurrency($code)
180
    {
181
        if (!$this->isError() && !$this->currency->update($code, $this->getSubmitted())) {
182
            $this->errorExit($this->text('Currency has not been updated'));
183
        }
184
    }
185
186
    /**
187
     * Add a currency at once
188
     */
189
    protected function submitAddCurrency()
190
    {
191
        $this->setSubmitted(null, $this->getParam());
192
        $this->validateComponent('currency');
193
        $this->addCurrency();
194
    }
195
196
    /**
197
     * Adds a currency step by step
198
     */
199
    protected function wizardAddCurrency()
200
    {
201
        // Required
202
        $this->validatePrompt('code', $this->text('Code'), 'currency');
203
        $this->validatePrompt('name', $this->text('Name'), 'currency');
204
        $this->validatePrompt('symbol', $this->text('Symbol'), 'currency');
205
        $this->validatePrompt('major_unit', $this->text('Major unit'), 'currency');
206
        $this->validatePrompt('minor_unit', $this->text('Minor unit'), 'currency');
207
        $this->validatePrompt('numeric_code', $this->text('Numeric code'), 'currency');
208
209
        // Optional
210
        $this->validatePrompt('status', $this->text('Status'), 'currency', 0);
211
        $this->validatePrompt('default', $this->text('Default'), 'currency', 0);
212
        $this->validatePrompt('decimals', $this->text('Decimals'), 'currency', 2);
213
        $this->validatePrompt('rounding_step', $this->text('Rounding step'), 'currency', 0);
214
        $this->validatePrompt('conversion_rate', $this->text('Conversion rate'), 'currency', 1);
215
        $this->validatePrompt('decimal_separator', $this->text('Decimal separator'), 'currency', '.');
216
        $this->validatePrompt('thousands_separator', $this->text('Thousands separator'), 'currency', ',');
217
        $this->validatePrompt('template', $this->text('Template'), 'currency', '%symbol%price');
218
219
        $this->validateComponent('currency');
220
        $this->addCurrency();
221
    }
222
223
}
224