Skip to content

Commit d7d162e

Browse files
clean up providers, minor improvements to dev ux
1 parent 157d34e commit d7d162e

File tree

9 files changed

+40
-45
lines changed

9 files changed

+40
-45
lines changed

src/Client/Bpost/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(
3030
HttpClientInterface $httpClient,
3131
RequestFactoryInterface $requestFactory,
3232
StreamFactoryInterface $streamFactory,
33-
string $baseUrl = 'https://pudo.bpost.be'
33+
string $baseUrl
3434
) {
3535
$this->httpClient = $httpClient;
3636
$this->requestFactory = $requestFactory;

src/Client/PostNL/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333
RequestFactoryInterface $requestFactory,
3434
StreamFactoryInterface $streamFactory,
3535
string $apiKey,
36-
string $baseUrl = 'https://api-sandbox.postnl.nl'
36+
string $baseUrl
3737
) {
3838
$this->httpClient = $httpClient;
3939
$this->requestFactory = $requestFactory;

src/Factory/PostNL/ServicePointQueryFactory.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function createServicePointQueryForOrder(OrderInterface $order): ServiceP
3131
}
3232

3333
if ($countryCode !== null) {
34-
$servicePointQuery->setCountryCode($countryCode);
34+
$servicePointQuery->setCountry($countryCode);
3535
}
3636

3737
if ($postCode !== null) {
@@ -41,7 +41,6 @@ public function createServicePointQueryForOrder(OrderInterface $order): ServiceP
4141
if ($city !== null) {
4242
$servicePointQuery->setCity($city);
4343
}
44-
4544
return $servicePointQuery;
4645
}
4746

@@ -56,7 +55,7 @@ public function createServicePointQueryForPickupPoint(PickupPointCode $pickupPoi
5655
public function createServicePointQueryForAllPickupPoints(string $countryCode, ?string $postalCode = null): ServicePointQueryInterface
5756
{
5857
$servicePointQuery = new ServicePointQuery();
59-
$servicePointQuery->setCountryCode($countryCode);
58+
$servicePointQuery->setCountry($countryCode);
6059

6160
if ($postalCode !== null) {
6261
$servicePointQuery->setPostalCode($postalCode);

src/Model/Query/Bpost/ServicePointQuery.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Setono\SyliusPickupPointPlugin\Model\Query\Bpost;
44

5-
final class ServicePointQuery implements ServicePointQueryInterface
5+
use Setono\SyliusPickupPointPlugin\Model\Query\CountryAwareInterface;
6+
7+
final class ServicePointQuery implements ServicePointQueryInterface, CountryAwareInterface
68
{
79
private const ENDPOINT = '/Locator';
810

src/Model/Query/Bpost/ServicePointQueryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ interface ServicePointQueryInterface extends BaseServicePointQueryInterface
2929
self::TYPE_SHOP => 8,
3030
self::TYPE_KARIBOO => 16,
3131
];
32+
33+
public function setType(int $type);
3234
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Setono\SyliusPickupPointPlugin\Model\Query;
4+
5+
interface CountryAwareInterface
6+
{
7+
public function getCountry(): string;
8+
9+
public function setCountry(string $country): void;
10+
}

src/Model/Query/PostNL/ServicePointQuery.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Setono\SyliusPickupPointPlugin\Model\Query\PostNL;
44

5+
use Setono\SyliusPickupPointPlugin\Model\Query\CountryAwareInterface;
56
use Setono\SyliusPickupPointPlugin\Model\Query\ServicePointQueryInterface;
67

7-
final class ServicePointQuery implements ServicePointQueryInterface
8+
final class ServicePointQuery implements ServicePointQueryInterface, CountryAwareInterface
89
{
910
private const ENDPOINT = '/shipment/v2_1/locations/nearest';
1011

@@ -46,14 +47,14 @@ public function setStreet(string $street): void
4647
$this->street = $street;
4748
}
4849

49-
public function getCountryCode(): string
50+
public function getCountry(): string
5051
{
5152
return $this->countryCode;
5253
}
5354

54-
public function setCountryCode(string $countryCode): void
55+
public function setCountry(string $country): void
5556
{
56-
$this->countryCode = $countryCode;
57+
$this->countryCode = $country;
5758
}
5859

5960
public function getPostalCode(): string
@@ -129,6 +130,7 @@ public function toArray(): array
129130
}
130131
$arrayValue[ucfirst($key)] = $value;
131132
}
133+
132134
return $arrayValue;
133135
}
134136
}

src/Provider/BpostProvider.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
use Setono\SyliusPickupPointPlugin\Client\ClientInterface;
77
use Setono\SyliusPickupPointPlugin\Factory\Bpost\ServicePointQueryFactory;
88
use Setono\SyliusPickupPointPlugin\Factory\ServicePointQueryFactoryInterface;
9-
use Setono\SyliusPickupPointPlugin\Model\Query\Bpost\ServicePointQueryInterface;
9+
use Setono\SyliusPickupPointPlugin\Model\Query\Bpost;
10+
use Setono\SyliusPickupPointPlugin\Model\Query\CountryAwareInterface;
1011
use Setono\SyliusPickupPointPlugin\Transformer\PickupPointTransformerInterface;
1112
use Setono\SyliusPickupPointPlugin\Exception\TimeoutException;
1213
use Setono\SyliusPickupPointPlugin\Model\PickupPointCode;
1314
use Setono\SyliusPickupPointPlugin\Model\PickupPointInterface;
1415
use Sylius\Component\Core\Model\OrderInterface;
1516

1617
/**
17-
* @see https://pudo.bpost.be/ServicePointQuery for more information
18+
* @see https://pudo.bpost.be/ for more information
1819
*/
1920
final class BpostProvider extends Provider
2021
{
@@ -44,20 +45,12 @@ public function __construct(
4445

4546
public function findPickupPoints(OrderInterface $order): iterable
4647
{
47-
$shippingAddress = $order->getShippingAddress();
48-
if (null === $shippingAddress) {
49-
return [];
50-
}
51-
52-
$countryCode = $shippingAddress->getCountryCode();
53-
if (null === $countryCode) {
54-
return [];
55-
}
56-
5748
$servicePointQuery = $this->getServicePointQueryFactory()->createServicePointQueryForOrder($order);
5849
$servicePoints = $this->client->locate($servicePointQuery);
5950
foreach ($servicePoints as $item) {
60-
$item['country'] = $countryCode;
51+
if ($servicePointQuery instanceof CountryAwareInterface) {
52+
$item['country'] = $servicePointQuery->getCountry();
53+
}
6154
yield $this->transform($item);
6255
}
6356
}
@@ -67,12 +60,16 @@ public function findPickupPoint(PickupPointCode $code): ?PickupPointInterface
6760
$servicePoints = [];
6861
try {
6962
$servicePointQuery = $this->getServicePointQueryFactory()->createServicePointQueryForPickupPoint($code);
70-
foreach (ServicePointQueryInterface::TYPES as $type) {
71-
$servicePointQuery->setType($type);
72-
$servicePoints = $this->client->locate($servicePointQuery);
73-
if (count($servicePoints) > 0) {
74-
break;
63+
if ($servicePointQuery instanceof Bpost\ServicePointQueryInterface) {
64+
foreach (Bpost\ServicePointQueryInterface::TYPES as $type) {
65+
$servicePointQuery->setType($type);
66+
$servicePoints = $this->client->locate($servicePointQuery);
67+
if (count($servicePoints) > 0) {
68+
break;
69+
}
7570
}
71+
} else {
72+
$servicePoints = $this->client->locate($servicePointQuery);
7673
}
7774
} catch (NetworkExceptionInterface $e) {
7875
throw new TimeoutException($e);
@@ -92,8 +89,6 @@ public function findAllPickupPoints(): iterable
9289
try {
9390
foreach ($this->countryCodes as $countryCode) {
9491
$servicePointQuery = $this->getServicePointQueryFactory()->createServicePointQueryForAllPickupPoints($countryCode);
95-
96-
$servicePointQuery->setCountry($countryCode);
9792
$servicePoints = $this->client->locate($servicePointQuery);
9893
foreach ($servicePoints as $item) {
9994
$item['country'] = $countryCode;

src/Provider/PostNLProvider.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,9 @@ public function __construct(
4949

5050
public function findPickupPoints(OrderInterface $order): iterable
5151
{
52-
$shippingAddress = $order->getShippingAddress();
53-
if (null === $shippingAddress) {
54-
return [];
55-
}
56-
57-
$countryCode = $shippingAddress->getCountryCode();
58-
if (null === $countryCode) {
59-
return [];
60-
}
61-
6252
$servicePointQuery = $this->getServicePointQueryFactory()->createServicePointQueryForOrder($order);
6353
$servicePoints = $this->client->locate($servicePointQuery);
6454
foreach ($servicePoints as $item) {
65-
$item['country'] = $countryCode;
6655
yield $this->transform($item);
6756
}
6857
}
@@ -75,16 +64,13 @@ public function findPickupPoint(PickupPointCode $code): ?PickupPointInterface
7564
} catch (NetworkExceptionInterface $e) {
7665
throw new TimeoutException($e);
7766
}
78-
7967
$servicePoints = [];
8068
foreach ($servicePoint as $index => $point) {
8169
$servicePoints[$index] = $point;
8270
}
83-
8471
if (\count($servicePoints) < 1) {
8572
return null;
8673
}
87-
8874
return $this->transform($servicePoints);
8975
}
9076

@@ -120,7 +106,6 @@ public function findAllPickupPoints(): iterable
120106
->createServicePointQueryForAllPickupPoints($countryCode, $postalCode);
121107
$servicePoints = $this->client->locate($servicePointQuery);
122108
foreach ($servicePoints as $item) {
123-
$item['country'] = $countryCode;
124109
yield $this->transform($item);
125110
}
126111
}

0 commit comments

Comments
 (0)