1616use Otherguy \Currency \Drivers \MockCurrencyDriver ;
1717use Otherguy \Currency \Drivers \OpenExchangeRates ;
1818use Otherguy \Currency \Exceptions \DriverNotFoundException ;
19+ use Otherguy \Currency \Exceptions \MissingDependencyException ;
1920use Psr \Http \Client \ClientInterface ;
2021use Psr \Http \Message \RequestFactoryInterface ;
21- use RuntimeException ;
2222
2323class DriverFactory
2424{
@@ -35,14 +35,14 @@ class DriverFactory
3535 public function __construct (?array $ drivers = null )
3636 {
3737 $ this ->drivers = $ drivers ?? [
38- 'mock ' => MockCurrencyDriver::class,
39- 'fixerio ' => FixerIo::class,
40- 'currencylayer ' => CurrencyLayer::class,
41- 'openexchangerates ' => OpenExchangeRates::class,
42- 'exchangeratesapi ' => ExchangeRatesApi::class,
43- 'frankfurter ' => Frankfurter::class,
44- 'currencyapi ' => CurrencyApi::class,
45- 'fastforex ' => FastForex::class,
38+ 'mock ' => MockCurrencyDriver::class,
39+ 'fixerio ' => FixerIo::class,
40+ 'currencylayer ' => CurrencyLayer::class,
41+ 'openexchangerates ' => OpenExchangeRates::class,
42+ 'exchangeratesapi ' => ExchangeRatesApi::class,
43+ 'frankfurter ' => Frankfurter::class,
44+ 'currencyapi ' => CurrencyApi::class,
45+ 'fastforex ' => FastForex::class,
4646 ];
4747 }
4848
@@ -124,24 +124,57 @@ public static function setDefault(?self $instance): void
124124 private function defaultClient (): ClientInterface
125125 {
126126 if (!class_exists (GuzzleClient::class)) {
127- throw new RuntimeException (
128- 'No PSR-18 HTTP client supplied and guzzlehttp/guzzle is not installed. '
129- . 'Either install guzzlehttp/guzzle, or pass a ClientInterface to DriverFactory::make(). ' ,
127+ throw new MissingDependencyException (
128+ 'No PSR-18 HTTP client supplied and guzzlehttp/guzzle is '
129+ . 'not installed. Either install guzzlehttp/guzzle, or pass '
130+ . 'a ClientInterface to DriverFactory::make(). ' ,
130131 );
131132 }
132133
133- return new GuzzleClient ();
134+ $ client = $ this ->buildDefaultClient ();
135+ if (!$ client instanceof ClientInterface) {
136+ throw new MissingDependencyException (
137+ 'The installed guzzlehttp/guzzle package does not provide a PSR-18 '
138+ . 'ClientInterface implementation. ' ,
139+ );
140+ }
141+
142+ return $ client ;
134143 }
135144
136145 private function defaultRequestFactory (): RequestFactoryInterface
137146 {
138147 if (!class_exists (GuzzleRequestFactory::class)) {
139- throw new RuntimeException (
140- 'No PSR-17 RequestFactory supplied and http-interop/http-factory-guzzle is not installed. '
141- . 'Either install http-interop/http-factory-guzzle, or pass a RequestFactoryInterface to DriverFactory::make(). ' ,
148+ throw new MissingDependencyException (
149+ 'No PSR-17 RequestFactory supplied and '
150+ . 'http-interop/http-factory-guzzle is not installed. '
151+ . 'Either install http-interop/http-factory-guzzle, or pass '
152+ . 'a RequestFactoryInterface to DriverFactory::make(). ' ,
142153 );
143154 }
144155
145- return new GuzzleRequestFactory ();
156+ $ requestFactory = $ this ->buildDefaultRequestFactory ();
157+ if (!$ requestFactory instanceof RequestFactoryInterface) {
158+ throw new MissingDependencyException (
159+ 'The installed http-interop/http-factory-guzzle package does not '
160+ . 'provide a PSR-17 RequestFactoryInterface implementation. ' ,
161+ );
162+ }
163+
164+ return $ requestFactory ;
165+ }
166+
167+ private function buildDefaultClient (): object
168+ {
169+ $ class = GuzzleClient::class;
170+
171+ return new $ class ();
172+ }
173+
174+ private function buildDefaultRequestFactory (): object
175+ {
176+ $ class = GuzzleRequestFactory::class;
177+
178+ return new $ class ();
146179 }
147180}
0 commit comments