diff --git a/src/Client/Bpost/Client.php b/src/Client/Bpost/Client.php index 1d1aed8..993efc9 100644 --- a/src/Client/Bpost/Client.php +++ b/src/Client/Bpost/Client.php @@ -30,7 +30,7 @@ public function __construct( HttpClientInterface $httpClient, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory, - string $baseUrl = 'https://pudo.bpost.be' + string $baseUrl ) { $this->httpClient = $httpClient; $this->requestFactory = $requestFactory; diff --git a/src/Client/PostNL/Client.php b/src/Client/PostNL/Client.php index 82ae381..329f960 100644 --- a/src/Client/PostNL/Client.php +++ b/src/Client/PostNL/Client.php @@ -33,7 +33,7 @@ public function __construct( RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory, string $apiKey, - string $baseUrl = 'https://api-sandbox.postnl.nl' + string $baseUrl ) { $this->httpClient = $httpClient; $this->requestFactory = $requestFactory; diff --git a/src/Factory/PostNL/ServicePointQueryFactory.php b/src/Factory/PostNL/ServicePointQueryFactory.php index 70b7058..6b36a2e 100644 --- a/src/Factory/PostNL/ServicePointQueryFactory.php +++ b/src/Factory/PostNL/ServicePointQueryFactory.php @@ -31,7 +31,7 @@ public function createServicePointQueryForOrder(OrderInterface $order): ServiceP } if ($countryCode !== null) { - $servicePointQuery->setCountryCode($countryCode); + $servicePointQuery->setCountry($countryCode); } if ($postCode !== null) { @@ -41,7 +41,6 @@ public function createServicePointQueryForOrder(OrderInterface $order): ServiceP if ($city !== null) { $servicePointQuery->setCity($city); } - return $servicePointQuery; } @@ -56,7 +55,7 @@ public function createServicePointQueryForPickupPoint(PickupPointCode $pickupPoi public function createServicePointQueryForAllPickupPoints(string $countryCode, ?string $postalCode = null): ServicePointQueryInterface { $servicePointQuery = new ServicePointQuery(); - $servicePointQuery->setCountryCode($countryCode); + $servicePointQuery->setCountry($countryCode); if ($postalCode !== null) { $servicePointQuery->setPostalCode($postalCode); diff --git a/src/Model/Query/Bpost/ServicePointQuery.php b/src/Model/Query/Bpost/ServicePointQuery.php index 0c703c3..c71ba36 100644 --- a/src/Model/Query/Bpost/ServicePointQuery.php +++ b/src/Model/Query/Bpost/ServicePointQuery.php @@ -2,7 +2,9 @@ namespace Setono\SyliusPickupPointPlugin\Model\Query\Bpost; -final class ServicePointQuery implements ServicePointQueryInterface +use Setono\SyliusPickupPointPlugin\Model\Query\CountryAwareInterface; + +final class ServicePointQuery implements ServicePointQueryInterface, CountryAwareInterface { private const ENDPOINT = '/Locator'; diff --git a/src/Model/Query/Bpost/ServicePointQueryInterface.php b/src/Model/Query/Bpost/ServicePointQueryInterface.php index 31d0fa1..b09a021 100644 --- a/src/Model/Query/Bpost/ServicePointQueryInterface.php +++ b/src/Model/Query/Bpost/ServicePointQueryInterface.php @@ -29,4 +29,6 @@ interface ServicePointQueryInterface extends BaseServicePointQueryInterface self::TYPE_SHOP => 8, self::TYPE_KARIBOO => 16, ]; + + public function setType(int $type); } diff --git a/src/Model/Query/CountryAwareInterface.php b/src/Model/Query/CountryAwareInterface.php new file mode 100644 index 0000000..6949f64 --- /dev/null +++ b/src/Model/Query/CountryAwareInterface.php @@ -0,0 +1,10 @@ +street = $street; } - public function getCountryCode(): string + public function getCountry(): string { return $this->countryCode; } - public function setCountryCode(string $countryCode): void + public function setCountry(string $country): void { - $this->countryCode = $countryCode; + $this->countryCode = $country; } public function getPostalCode(): string @@ -129,6 +130,7 @@ public function toArray(): array } $arrayValue[ucfirst($key)] = $value; } + return $arrayValue; } } diff --git a/src/Provider/BpostProvider.php b/src/Provider/BpostProvider.php index 0530865..911f4cf 100644 --- a/src/Provider/BpostProvider.php +++ b/src/Provider/BpostProvider.php @@ -6,7 +6,8 @@ use Setono\SyliusPickupPointPlugin\Client\ClientInterface; use Setono\SyliusPickupPointPlugin\Factory\Bpost\ServicePointQueryFactory; use Setono\SyliusPickupPointPlugin\Factory\ServicePointQueryFactoryInterface; -use Setono\SyliusPickupPointPlugin\Model\Query\Bpost\ServicePointQueryInterface; +use Setono\SyliusPickupPointPlugin\Model\Query\Bpost; +use Setono\SyliusPickupPointPlugin\Model\Query\CountryAwareInterface; use Setono\SyliusPickupPointPlugin\Transformer\PickupPointTransformerInterface; use Setono\SyliusPickupPointPlugin\Exception\TimeoutException; use Setono\SyliusPickupPointPlugin\Model\PickupPointCode; @@ -14,7 +15,7 @@ use Sylius\Component\Core\Model\OrderInterface; /** - * @see https://pudo.bpost.be/ServicePointQuery for more information + * @see https://pudo.bpost.be/ for more information */ final class BpostProvider extends Provider { @@ -44,20 +45,12 @@ public function __construct( public function findPickupPoints(OrderInterface $order): iterable { - $shippingAddress = $order->getShippingAddress(); - if (null === $shippingAddress) { - return []; - } - - $countryCode = $shippingAddress->getCountryCode(); - if (null === $countryCode) { - return []; - } - $servicePointQuery = $this->getServicePointQueryFactory()->createServicePointQueryForOrder($order); $servicePoints = $this->client->locate($servicePointQuery); foreach ($servicePoints as $item) { - $item['country'] = $countryCode; + if ($servicePointQuery instanceof CountryAwareInterface) { + $item['country'] = $servicePointQuery->getCountry(); + } yield $this->transform($item); } } @@ -67,12 +60,16 @@ public function findPickupPoint(PickupPointCode $code): ?PickupPointInterface $servicePoints = []; try { $servicePointQuery = $this->getServicePointQueryFactory()->createServicePointQueryForPickupPoint($code); - foreach (ServicePointQueryInterface::TYPES as $type) { - $servicePointQuery->setType($type); - $servicePoints = $this->client->locate($servicePointQuery); - if (count($servicePoints) > 0) { - break; + if ($servicePointQuery instanceof Bpost\ServicePointQueryInterface) { + foreach (Bpost\ServicePointQueryInterface::TYPES as $type) { + $servicePointQuery->setType($type); + $servicePoints = $this->client->locate($servicePointQuery); + if (count($servicePoints) > 0) { + break; + } } + } else { + $servicePoints = $this->client->locate($servicePointQuery); } } catch (NetworkExceptionInterface $e) { throw new TimeoutException($e); @@ -92,8 +89,6 @@ public function findAllPickupPoints(): iterable try { foreach ($this->countryCodes as $countryCode) { $servicePointQuery = $this->getServicePointQueryFactory()->createServicePointQueryForAllPickupPoints($countryCode); - - $servicePointQuery->setCountry($countryCode); $servicePoints = $this->client->locate($servicePointQuery); foreach ($servicePoints as $item) { $item['country'] = $countryCode; diff --git a/src/Provider/PostNLProvider.php b/src/Provider/PostNLProvider.php index 8989490..85c0f49 100644 --- a/src/Provider/PostNLProvider.php +++ b/src/Provider/PostNLProvider.php @@ -49,20 +49,9 @@ public function __construct( public function findPickupPoints(OrderInterface $order): iterable { - $shippingAddress = $order->getShippingAddress(); - if (null === $shippingAddress) { - return []; - } - - $countryCode = $shippingAddress->getCountryCode(); - if (null === $countryCode) { - return []; - } - $servicePointQuery = $this->getServicePointQueryFactory()->createServicePointQueryForOrder($order); $servicePoints = $this->client->locate($servicePointQuery); foreach ($servicePoints as $item) { - $item['country'] = $countryCode; yield $this->transform($item); } } @@ -75,16 +64,13 @@ public function findPickupPoint(PickupPointCode $code): ?PickupPointInterface } catch (NetworkExceptionInterface $e) { throw new TimeoutException($e); } - $servicePoints = []; foreach ($servicePoint as $index => $point) { $servicePoints[$index] = $point; } - if (\count($servicePoints) < 1) { return null; } - return $this->transform($servicePoints); } @@ -120,7 +106,6 @@ public function findAllPickupPoints(): iterable ->createServicePointQueryForAllPickupPoints($countryCode, $postalCode); $servicePoints = $this->client->locate($servicePointQuery); foreach ($servicePoints as $item) { - $item['country'] = $countryCode; yield $this->transform($item); } }