Skip to content

Commit

Permalink
clean up providers, minor improvements to dev ux
Browse files Browse the repository at this point in the history
  • Loading branch information
kenny-inventis committed May 24, 2022
1 parent 157d34e commit d7d162e
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Client/Bpost/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Client/PostNL/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/Factory/PostNL/ServicePointQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function createServicePointQueryForOrder(OrderInterface $order): ServiceP
}

if ($countryCode !== null) {
$servicePointQuery->setCountryCode($countryCode);
$servicePointQuery->setCountry($countryCode);
}

if ($postCode !== null) {
Expand All @@ -41,7 +41,6 @@ public function createServicePointQueryForOrder(OrderInterface $order): ServiceP
if ($city !== null) {
$servicePointQuery->setCity($city);
}

return $servicePointQuery;
}

Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/Model/Query/Bpost/ServicePointQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 2 additions & 0 deletions src/Model/Query/Bpost/ServicePointQueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ interface ServicePointQueryInterface extends BaseServicePointQueryInterface
self::TYPE_SHOP => 8,
self::TYPE_KARIBOO => 16,
];

public function setType(int $type);
}
10 changes: 10 additions & 0 deletions src/Model/Query/CountryAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Setono\SyliusPickupPointPlugin\Model\Query;

interface CountryAwareInterface
{
public function getCountry(): string;

public function setCountry(string $country): void;
}
10 changes: 6 additions & 4 deletions src/Model/Query/PostNL/ServicePointQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Setono\SyliusPickupPointPlugin\Model\Query\PostNL;

use Setono\SyliusPickupPointPlugin\Model\Query\CountryAwareInterface;
use Setono\SyliusPickupPointPlugin\Model\Query\ServicePointQueryInterface;

final class ServicePointQuery implements ServicePointQueryInterface
final class ServicePointQuery implements ServicePointQueryInterface, CountryAwareInterface
{
private const ENDPOINT = '/shipment/v2_1/locations/nearest';

Expand Down Expand Up @@ -46,14 +47,14 @@ public function setStreet(string $street): void
$this->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
Expand Down Expand Up @@ -129,6 +130,7 @@ public function toArray(): array
}
$arrayValue[ucfirst($key)] = $value;
}

return $arrayValue;
}
}
35 changes: 15 additions & 20 deletions src/Provider/BpostProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
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;
use Setono\SyliusPickupPointPlugin\Model\PickupPointInterface;
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
{
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand All @@ -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;
Expand Down
15 changes: 0 additions & 15 deletions src/Provider/PostNLProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}

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

0 comments on commit d7d162e

Please sign in to comment.