Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/MyParcelComApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}

Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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),
),
);

Expand Down Expand Up @@ -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).
Expand Down