Issues (52)

src/Commands/CurrencyManage.php (5 issues)

1
<?php
2
3
4
namespace Turahe\Master\Commands;
5
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Arr;
8
9
class CurrencyManage extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'currency:manage
17
                                {action : Action to perform (add, update, or delete)}
18
                                {currency : Code or comma separated list of codes for currencies}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Manage currency values';
26
27
    /**
28
     * Currency storage instance
29
     *
30
     * @var \Torann\Currency\Contracts\DriverInterface
0 ignored issues
show
The type Torann\Currency\Contracts\DriverInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
     */
32
    protected $storage;
33
34
    /**
35
     * All installable currencies.
36
     *
37
     * @var array
38
     */
39
    protected $currencies;
40
41
    /**
42
     * Create a new command instance.
43
     */
44
    public function __construct()
45
    {
46
        $this->storage = app('currency')->getDriver();
0 ignored issues
show
The method getDriver() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        $this->storage = app('currency')->/** @scrutinizer ignore-call */ getDriver();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        $this->currencies = include(__DIR__ . '/../../resources/currencies.php');
48
49
        parent::__construct();
50
    }
51
52
    /**
53
     * Execute the console command for Laravel 5.4 and below
54
     *
55
     * @return void
56
     */
57
    public function fire()
58
    {
59
        $this->handle();
60
    }
61
62
    /**
63
     * Execute the console command.
64
     *
65
     * @return void
66
     */
67
    public function handle()
68
    {
69
        $action = $this->getActionArgument(['add', 'update', 'delete']);
70
71
        foreach ($this->getCurrencyArgument() as $currency) {
72
            $this->$action(strtoupper($currency));
73
        }
74
    }
75
76
    /**
77
     * Add currency to storage.
78
     *
79
     * @param string $currency
80
     *
81
     * @return void
82
     */
83
    protected function add($currency)
84
    {
85
        if (($data = $this->getCurrency($currency)) === null) {
0 ignored issues
show
The condition $data = $this->getCurrency($currency) === null is always false.
Loading history...
86
            $this->error("Currency \"{$currency}\" not found");
87
            return;
88
        }
89
90
        $this->output->write("Adding {$currency} currency...");
91
92
        $data['code'] = $currency;
93
94
        if (is_string($result = $this->storage->create($data))) {
95
            $this->output->writeln('<error>' . ($result ?: 'Failed') . '</error>');
96
        } else {
97
            $this->output->writeln("<info>success</info>");
98
        }
99
    }
100
101
    /**
102
     * Update currency in storage.
103
     *
104
     * @param string $currency
105
     *
106
     * @return void
107
     */
108
    protected function update($currency)
109
    {
110
        if (($data = $this->getCurrency($currency)) === null) {
0 ignored issues
show
The condition $data = $this->getCurrency($currency) === null is always false.
Loading history...
111
            $this->error("Currency \"{$currency}\" not found");
112
            return;
113
        }
114
115
        $this->output->write("Updating {$currency} currency...");
116
117
        if (is_string($result = $this->storage->update($currency, $data))) {
118
            $this->output->writeln('<error>' . ($result ?: 'Failed') . '</error>');
119
        } else {
120
            $this->output->writeln("<info>success</info>");
121
        }
122
    }
123
124
    /**
125
     * Delete currency from storage.
126
     *
127
     * @param string $currency
128
     *
129
     * @return void
130
     */
131
    protected function delete($currency)
132
    {
133
        $this->output->write("Deleting {$currency} currency...");
134
135
        if (is_string($result = $this->storage->delete($currency))) {
136
            $this->output->writeln('<error>' . ($result ?: 'Failed') . '</error>');
137
        } else {
138
            $this->output->writeln("<info>success</info>");
139
        }
140
    }
141
142
    /**
143
     * Get currency argument.
144
     *
145
     * @return array
146
     */
147
    protected function getCurrencyArgument()
148
    {
149
        // Get the user entered value
150
        $value = preg_replace('/\s+/', '', $this->argument('currency'));
151
152
        // Return all currencies if requested
153
        if ($value === 'all') {
154
            return array_keys($this->currencies);
155
        }
156
157
        return explode(',', $value);
158
    }
159
160
    /**
161
     * Get action argument.
162
     *
163
     * @param array $validActions
164
     *
165
     * @return array
166
     */
167
    protected function getActionArgument($validActions = [])
168
    {
169
        $action = strtolower($this->argument('action'));
0 ignored issues
show
It seems like $this->argument('action') can also be of type string[]; however, parameter $str of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

169
        $action = strtolower(/** @scrutinizer ignore-type */ $this->argument('action'));
Loading history...
170
171
        if (in_array($action, $validActions) === false) {
172
            throw new \RuntimeException("The \"{$action}\" option does not exist.");
173
        }
174
175
        return $action;
176
    }
177
178
    /**
179
     * Get currency data.
180
     *
181
     * @param string $currency
182
     *
183
     * @return array
184
     */
185
    protected function getCurrency($currency)
186
    {
187
        return Arr::get($this->currencies, $currency);
188
    }
189
}
190