diff --git a/src/MyParcelComApi.php b/src/MyParcelComApi.php index ce2f17d..085046f 100644 --- a/src/MyParcelComApi.php +++ b/src/MyParcelComApi.php @@ -55,8 +55,8 @@ class MyParcelComApi implements MyParcelComApiInterface protected ResourceFactoryInterface $resourceFactory; protected AuthenticatorInterface $authenticator; private ClientInterface $client; + private ServiceMatcher $serviceMatcher; private bool $authRetry = false; - private static ?MyParcelComApi $singleton = null; /** @@ -68,8 +68,9 @@ public static function createSingleton( ClientInterface $httpClient = null, CacheInterface $cache = null, ResourceFactoryInterface $resourceFactory = null, + ServiceMatcher $serviceMatcher = null, ): self { - return self::$singleton = (new self($apiUri, $httpClient, $cache, $resourceFactory)) + return self::$singleton = (new self($apiUri, $httpClient, $cache, $resourceFactory, $serviceMatcher)) ->authenticate($authenticator); } @@ -90,6 +91,7 @@ public function __construct( ClientInterface $httpClient = null, CacheInterface $cache = null, ResourceFactoryInterface $resourceFactory = null, + ServiceMatcher $serviceMatcher = null, ) { if ($httpClient === null) { $httpClient = HttpClientDiscovery::find(); @@ -108,6 +110,11 @@ public function __construct( // Either use the given resource factory or instantiate a new one. $this->setResourceFactory($resourceFactory ?: new ResourceFactory()); + + if ($serviceMatcher === null) { + $serviceMatcher = new ServiceMatcher(); + } + $this->setServiceMatcher($serviceMatcher); } public function authenticate(AuthenticatorInterface $authenticator): self @@ -266,11 +273,10 @@ public function getServices( $services = $this->getResourcesArray($url->getUrl(), $ttl); - $matcher = new ServiceMatcher(); $services = array_values( array_filter( $services, - fn (ServiceInterface $service) => $matcher->matchesDeliveryMethod($shipment, $service), + fn (ServiceInterface $service) => $this->serviceMatcher->matchesDeliveryMethod($shipment, $service), ), ); @@ -1039,6 +1045,13 @@ protected function getHttpClient(): ClientInterface return $this->client; } + public function setServiceMatcher(ServiceMatcher $serviceMatcher): self + { + $this->serviceMatcher = $serviceMatcher; + + return $this; + } + /** * Get a promise that will return an array with resources requested from given uri. * A time-to-live can be specified for how long this request should be cached (defaults to 10 minutes).