-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDriverFactory.php
More file actions
180 lines (152 loc) · 5.35 KB
/
DriverFactory.php
File metadata and controls
180 lines (152 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
namespace Otherguy\Currency;
use GuzzleHttp\Client as GuzzleClient;
use Http\Factory\Guzzle\RequestFactory as GuzzleRequestFactory;
use Otherguy\Currency\Drivers\CurrencyApi;
use Otherguy\Currency\Drivers\CurrencyDriverContract;
use Otherguy\Currency\Drivers\CurrencyLayer;
use Otherguy\Currency\Drivers\ExchangeRatesApi;
use Otherguy\Currency\Drivers\FastForex;
use Otherguy\Currency\Drivers\FixerIo;
use Otherguy\Currency\Drivers\Frankfurter;
use Otherguy\Currency\Drivers\MockCurrencyDriver;
use Otherguy\Currency\Drivers\OpenExchangeRates;
use Otherguy\Currency\Exceptions\DriverNotFoundException;
use Otherguy\Currency\Exceptions\MissingDependencyException;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
class DriverFactory
{
/**
* @var array<string, class-string<CurrencyDriverContract>>
*/
private array $drivers;
private static ?self $defaultInstance = null;
/**
* @param array<string, class-string<CurrencyDriverContract>>|null $drivers
*/
public function __construct(?array $drivers = null)
{
$this->drivers = $drivers ?? [
'mock' => MockCurrencyDriver::class,
'fixerio' => FixerIo::class,
'currencylayer' => CurrencyLayer::class,
'openexchangerates' => OpenExchangeRates::class,
'exchangeratesapi' => ExchangeRatesApi::class,
'frankfurter' => Frankfurter::class,
'currencyapi' => CurrencyApi::class,
'fastforex' => FastForex::class,
];
}
/**
* @param class-string<CurrencyDriverContract> $driverClass
*/
public function register(string $name, string $driverClass): self
{
$this->drivers[$name] = $driverClass;
return $this;
}
public function unregister(string $name): self
{
unset($this->drivers[$name]);
return $this;
}
/**
* @return array<string, class-string<CurrencyDriverContract>>
*/
public function drivers(): array
{
return $this->drivers;
}
/**
* @throws DriverNotFoundException
*/
public function build(
string $name,
?ClientInterface $httpClient = null,
?RequestFactoryInterface $requestFactory = null,
): CurrencyDriverContract {
if (!isset($this->drivers[$name])) {
throw new DriverNotFoundException("{$name} is not a valid driver.");
}
$class = $this->drivers[$name];
$client = $httpClient ?? $this->defaultClient();
$factory = $requestFactory ?? $this->defaultRequestFactory();
return new $class($client, $factory);
}
/**
* Static facade preserved for backwards compatibility.
*
* @throws DriverNotFoundException
*/
public static function make(
string $name,
?ClientInterface $httpClient = null,
?RequestFactoryInterface $requestFactory = null,
): CurrencyDriverContract {
return self::default()->build($name, $httpClient, $requestFactory);
}
/**
* @return array<string, class-string<CurrencyDriverContract>>
*/
public static function getDrivers(): array
{
return self::default()->drivers();
}
public static function default(): self
{
return self::$defaultInstance ??= new self();
}
public static function setDefault(?self $instance): void
{
self::$defaultInstance = $instance;
}
private function defaultClient(): ClientInterface
{
if (!class_exists(GuzzleClient::class)) {
throw new MissingDependencyException(
'No PSR-18 HTTP client supplied and guzzlehttp/guzzle is '
. 'not installed. Either install guzzlehttp/guzzle, or pass '
. 'a ClientInterface to DriverFactory::make().',
);
}
$client = $this->buildDefaultClient();
if (!$client instanceof ClientInterface) {
throw new MissingDependencyException(
'The installed guzzlehttp/guzzle package does not provide a PSR-18 '
. 'ClientInterface implementation.',
);
}
return $client;
}
private function defaultRequestFactory(): RequestFactoryInterface
{
if (!class_exists(GuzzleRequestFactory::class)) {
throw new MissingDependencyException(
'No PSR-17 RequestFactory supplied and '
. 'http-interop/http-factory-guzzle is not installed. '
. 'Either install http-interop/http-factory-guzzle, or pass '
. 'a RequestFactoryInterface to DriverFactory::make().',
);
}
$requestFactory = $this->buildDefaultRequestFactory();
if (!$requestFactory instanceof RequestFactoryInterface) {
throw new MissingDependencyException(
'The installed http-interop/http-factory-guzzle package does not '
. 'provide a PSR-17 RequestFactoryInterface implementation.',
);
}
return $requestFactory;
}
private function buildDefaultClient(): object
{
$class = GuzzleClient::class;
return new $class();
}
private function buildDefaultRequestFactory(): object
{
$class = GuzzleRequestFactory::class;
return new $class();
}
}