14
14
use Sylius \Bundle \ResourceBundle \Doctrine \ORM \EntityRepository ;
15
15
use Sylius \Component \Core \Model \OrderInterface ;
16
16
use Webmozart \Assert \Assert ;
17
+ use Sylius \Component \Core \Model \Address ;
17
18
18
19
/**
19
20
* @see https://developer.postnl.nl/browse-apis/delivery-options/location-webservice/testtool-rest/#/default/get_v2_1_locations_nearest
@@ -26,7 +27,7 @@ final class PostNLProvider extends Provider
26
27
private ClientInterface $ client ;
27
28
private PickupPointTransformerInterface $ pickupPointTransformer ;
28
29
private ManagerRegistry $ managerRegistry ;
29
- private ?ServicePointQueryFactory $ servicePointQueryFactory ;
30
+ private ?ServicePointQueryFactoryInterface $ servicePointQueryFactory ;
30
31
31
32
private array $ countryCodes ;
32
33
private string $ addressClassString ;
@@ -35,8 +36,8 @@ public function __construct(
35
36
ClientInterface $ client ,
36
37
PickupPointTransformerInterface $ pickupPointTransformer ,
37
38
ManagerRegistry $ managerRegistry ,
38
- string $ addressClassString ,
39
39
?ServicePointQueryFactoryInterface $ servicePointQueryFactory = null ,
40
+ string $ addressClassString = Address::class,
40
41
array $ countryCodes = ['BE ' , 'NL ' ]
41
42
) {
42
43
$ this ->client = $ client ;
@@ -90,6 +91,7 @@ public function findAllPickupPoints(): iterable
90
91
/** @var EntityRepository $repository */
91
92
$ repository = $ manager ->getRepository ($ this ->addressClassString );
92
93
try {
94
+ $ alreadyScannedPoints = [];
93
95
foreach ($ this ->countryCodes as $ countryCode ) {
94
96
$ qb = $ repository ->createQueryBuilder ('sa ' );
95
97
$ postalCodes = $ qb ->distinct ()->select ('sa.postcode ' )
@@ -106,7 +108,14 @@ public function findAllPickupPoints(): iterable
106
108
->createServicePointQueryForAllPickupPoints ($ countryCode , $ postalCode );
107
109
$ servicePoints = $ this ->client ->locate ($ servicePointQuery );
108
110
foreach ($ servicePoints as $ item ) {
109
- yield $ this ->transform ($ item );
111
+ $ pickupPoint = $ this ->transform ($ item );
112
+ $ code = $ pickupPoint ->getCode ();
113
+ if ($ code === null || in_array ($ code , $ alreadyScannedPoints , true )) {
114
+ continue ;
115
+ }
116
+ // Prevent the same adjacent pickup points from being added multiple times
117
+ $ alreadyScannedPoints [] = $ code ->getValue ();
118
+ yield $ pickupPoint ;
110
119
}
111
120
}
112
121
}
0 commit comments