LaravelCryptoStatsFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 23 5
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
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

23
        $connectors = /** @scrutinizer ignore-call */ app()->tagged('laravel-crypto-stats.connectors');
Loading history...
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
Bug Best Practice introduced by
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 empty(..) or ! empty(...) instead.

Loading history...
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