1 | <?php |
||
2 | |||
3 | namespace LaravelCryptoStats; |
||
4 | |||
5 | use Exception; |
||
6 | |||
7 | class LaravelCryptoStatsFactory |
||
8 | { |
||
9 | /** |
||
10 | * Array of the existed API connectors. |
||
11 | * |
||
12 | * @var array |
||
13 | */ |
||
14 | public $connectors; |
||
15 | |||
16 | /** |
||
17 | * LaravelCryptoStatsFactory buider. |
||
18 | * |
||
19 | * Get the array of the API connector classes and write it to the $connectors variable |
||
20 | */ |
||
21 | public function __construct() |
||
22 | { |
||
23 | $connectors = app()->tagged('laravel-crypto-stats.connectors'); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
24 | $this->connectors = $connectors; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Get the instance of the needed API connector class. |
||
29 | * |
||
30 | * @param string $currency - currency for which the API connector instance will be created |
||
31 | * |
||
32 | * @throws Exception |
||
33 | * |
||
34 | * @return \LaravelCryptoStats\connector - instance of the needed API connector class |
||
35 | */ |
||
36 | public function getInstance($currency) |
||
37 | { |
||
38 | $connectors = $this->connectors; |
||
39 | if ($connectors) { |
||
0 ignored issues
–
show
The expression
$connectors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
40 | $supported_currencies = []; |
||
41 | $instance = []; |
||
42 | |||
43 | foreach ($connectors as $connector) { |
||
44 | $connector->setCurrency($currency); |
||
45 | $supported_currencies = array_merge($supported_currencies, $connector->supported_currencies); |
||
46 | |||
47 | if (in_array($currency, $connector->supported_currencies)) { |
||
48 | $instance = $connector; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | if ($instance) { |
||
53 | return $instance; |
||
54 | } |
||
55 | |||
56 | $supported_currencies = implode(', ', $supported_currencies); |
||
57 | |||
58 | throw new Exception('"'.$currency.'" cryptocurrency is not supported now! Currently available values: '.$supported_currencies); |
||
59 | } |
||
60 | } |
||
61 | } |
||
62 |