CurrencyRates   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 173
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A callCustomCreator() 0 3 1
A driver() 0 9 3
A createDummyDriver() 0 3 1
A getDrivers() 0 3 1
A createYahooDriver() 0 3 1
A studlyCase() 0 4 1
A getDefaultDriver() 0 3 1
A __call() 0 3 1
A createDriver() 0 14 3
A extend() 0 5 1
A createFixerDriver() 0 3 1
A setDefaultDriver() 0 5 1
1
<?php
2
3
namespace Ultraleet\CurrencyRates;
4
5
use Ultraleet\CurrencyRates\Contracts\Factory;
6
use Ultraleet\CurrencyRates\Providers\DummyProvider;
7
use Ultraleet\CurrencyRates\Providers\FixerProvider;
8
use Ultraleet\CurrencyRates\Providers\YahooProvider;
9
use GuzzleHttp\Client as GuzzleClient;
10
use Closure;
11
use InvalidArgumentException;
12
13
class CurrencyRates implements Factory
14
{
15
    /**
16
     * The registered custom driver creators.
17
     *
18
     * @var array
19
     */
20
    protected $customCreators = [];
21
22
    /**
23
     * The array of created drivers.
24
     *
25
     * @var array
26
     */
27
    protected $drivers = [];
28
29
    /**
30
     * The name of the default driver.
31
     *
32
     * @var string
33
     */
34
    protected $defaultDriver = 'fixer';
35
36
    /**
37
     * Create an instance of the specified driver.
38
     *
39
     * @return \Ultraleet\CurrencyRates\AbstractProvider
40
     */
41
    protected function createFixerDriver()
42
    {
43
        return new FixerProvider(new GuzzleClient());
44
    }
45
46
    /**
47
     * Create an instance of the specified driver.
48
     *
49
     * @return \Ultraleet\CurrencyRates\AbstractProvider
50
     */
51
    protected function createYahooDriver()
52
    {
53
        return new YahooProvider(new GuzzleClient());
54
    }
55
56
    /**
57
     * Create an instance of the specified driver.
58
     *
59
     * @return \Ultraleet\CurrencyRates\AbstractProvider
60
     */
61
    protected function createDummyDriver()
62
    {
63
        return new DummyProvider();
64
    }
65
66
    /**
67
     * Get the default driver name.
68
     *
69
     * @return string
70
     */
71
    public function getDefaultDriver()
72
    {
73
        return $this->defaultDriver;
74
    }
75
76
    /**
77
     * Set the default driver name.
78
     *
79
     * @param string
80
     */
81
    public function setDefaultDriver($name)
82
    {
83
        $this->defaultDriver = $name;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Get a driver instance.
90
     *
91
     * @param  string  $driver
92
     * @return mixed
93
     */
94
    public function driver($driver = null)
95
    {
96
        $driver = $driver ?: $this->getDefaultDriver();
97
98
        if (!isset($this->drivers[$driver])) {
99
            $this->drivers[$driver] = $this->createDriver($driver);
100
        }
101
102
        return $this->drivers[$driver];
103
    }
104
105
    /**
106
     * Create a new driver instance.
107
     *
108
     * @param  string  $driver
109
     * @return mixed
110
     *
111
     * @throws \InvalidArgumentException
112
     */
113
    protected function createDriver($driver)
114
    {
115
        // First, we will check if a custom creator has been created for the
116
        // specified driver. If not, we will create a native driver instead.
117
        if (isset($this->customCreators[$driver])) {
118
            return $this->callCustomCreator($driver);
119
        } else {
120
            $method = 'create' . static::studlyCase($driver) . 'Driver';
121
122
            if (method_exists($this, $method)) {
123
                return $this->$method();
124
            }
125
        }
126
        throw new InvalidArgumentException("Driver [$driver] not supported.");
127
    }
128
129
    /**
130
     * Call a custom driver creator.
131
     *
132
     * @param  string  $driver
133
     * @return mixed
134
     */
135
    protected function callCustomCreator($driver)
136
    {
137
        return $this->customCreators[$driver]();
138
    }
139
140
    /**
141
     * Register a custom driver creator Closure.
142
     *
143
     * @param  string    $driver
144
     * @param  \Closure  $callback
145
     * @return $this
146
     */
147
    public function extend($driver, Closure $callback)
148
    {
149
        $this->customCreators[$driver] = $callback;
150
151
        return $this;
152
    }
153
154
    /**
155
     * Get all of the created drivers.
156
     *
157
     * @return array
158
     */
159
    public function getDrivers()
160
    {
161
        return $this->drivers;
162
    }
163
164
    /**
165
     * Dynamically call the default driver instance.
166
     *
167
     * @param  string  $method
168
     * @param  array   $parameters
169
     * @return mixed
170
     */
171
    public function __call($method, $parameters)
172
    {
173
        return $this->driver()->$method(...$parameters);
174
    }
175
176
    /**
177
     * Convert a value to studly caps case.
178
     *
179
     * @param  string  $value
180
     * @return string
181
     */
182
    public static function studlyCase($value)
183
    {
184
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
185
        return str_replace(' ', '', $value);
186
    }
187
}
188