1414use Sylius \Bundle \ResourceBundle \Doctrine \ORM \EntityRepository ;
1515use Sylius \Component \Core \Model \OrderInterface ;
1616use Webmozart \Assert \Assert ;
17+ use Sylius \Component \Core \Model \Address ;
1718
1819/**
1920 * @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
2627 private ClientInterface $ client ;
2728 private PickupPointTransformerInterface $ pickupPointTransformer ;
2829 private ManagerRegistry $ managerRegistry ;
29- private ?ServicePointQueryFactory $ servicePointQueryFactory ;
30+ private ?ServicePointQueryFactoryInterface $ servicePointQueryFactory ;
3031
3132 private array $ countryCodes ;
3233 private string $ addressClassString ;
@@ -35,8 +36,8 @@ public function __construct(
3536 ClientInterface $ client ,
3637 PickupPointTransformerInterface $ pickupPointTransformer ,
3738 ManagerRegistry $ managerRegistry ,
38- string $ addressClassString ,
3939 ?ServicePointQueryFactoryInterface $ servicePointQueryFactory = null ,
40+ string $ addressClassString = Address::class,
4041 array $ countryCodes = ['BE ' , 'NL ' ]
4142 ) {
4243 $ this ->client = $ client ;
@@ -90,6 +91,7 @@ public function findAllPickupPoints(): iterable
9091 /** @var EntityRepository $repository */
9192 $ repository = $ manager ->getRepository ($ this ->addressClassString );
9293 try {
94+ $ alreadyScannedPoints = [];
9395 foreach ($ this ->countryCodes as $ countryCode ) {
9496 $ qb = $ repository ->createQueryBuilder ('sa ' );
9597 $ postalCodes = $ qb ->distinct ()->select ('sa.postcode ' )
@@ -106,7 +108,14 @@ public function findAllPickupPoints(): iterable
106108 ->createServicePointQueryForAllPickupPoints ($ countryCode , $ postalCode );
107109 $ servicePoints = $ this ->client ->locate ($ servicePointQuery );
108110 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 ;
110119 }
111120 }
112121 }
0 commit comments