Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade post nord provider #106

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"symfony/http-foundation": "^5.4 || ^6.0",
"symfony/http-kernel": "^5.4 || ^6.0",
"symfony/intl": "^5.4 || ^6.0",
"symfony/mailer": "^5.4 || ^6.0",
"symfony/messenger": "^5.4 || ^6.0",
"symfony/options-resolver": "^5.4 || ^6.0",
"symfony/routing": "^5.4 || ^6.0",
Expand Down Expand Up @@ -55,7 +56,8 @@
"setono/coolrunner-bundle": "^1.0",
"setono/dao-bundle": "^1.1",
"setono/gls-webservice-bundle": "^1.3",
"setono/post-nord-bundle": "^1.0",
"setono/post-nord-bundle": "^2.0@alpha",
"setono/post-nord-php-sdk": "^2.0@alpha",
"setono/sylius-behat-pack": "^0.2",
"sylius/sylius": "~1.12.13",
"symfony/debug-bundle": "^5.4 || ^6.4",
Expand Down Expand Up @@ -89,10 +91,10 @@
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
"symfony/thanks": false,
"dealerdirect/phpcodesniffer-composer-installer": false,
"php-http/discovery": true
"ergebnis/composer-normalize": true,
"php-http/discovery": true,
"symfony/thanks": false
},
"sort-packages": true
},
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<directory name="tests"/>
<ignoreFiles>
<directory name="tests/Application"/>
<directory name="tests/Behat"/>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
Expand Down
118 changes: 47 additions & 71 deletions src/Provider/PostNordProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Setono\SyliusPickupPointPlugin\Provider;

use function preg_replace;
use Psr\Http\Client\NetworkExceptionInterface;
use Setono\PostNord\Client\ClientInterface;
use Setono\PostNord\Request\Query\ServicePoints\ByIdsQuery;
use Setono\PostNord\Request\Query\ServicePoints\NearestByAddressQuery;
use Setono\PostNord\Response\ServicePoints\ServicePoint;
use Setono\SyliusPickupPointPlugin\Exception\TimeoutException;
use Setono\SyliusPickupPointPlugin\Model\PickupPointCode;
use Setono\SyliusPickupPointPlugin\Model\PickupPointInterface;
Expand All @@ -19,14 +21,10 @@
*/
final class PostNordProvider extends Provider
{
private ClientInterface $client;

private FactoryInterface $pickupPointFactory;

public function __construct(ClientInterface $client, FactoryInterface $pickupPointFactory)
{
$this->client = $client;
$this->pickupPointFactory = $pickupPointFactory;
public function __construct(

Check warning on line 24 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L24

Added line #L24 was not covered by tests
private readonly ClientInterface $client,
private readonly FactoryInterface $pickupPointFactory,
) {
}

public function findPickupPoints(OrderInterface $order): iterable
Expand All @@ -37,30 +35,43 @@
}

$street = $shippingAddress->getStreet();
if (null === $street) {
return [];

Check warning on line 39 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L38-L39

Added lines #L38 - L39 were not covered by tests
}

$streetParts = explode(' ', $street);
if (count($streetParts) < 2) {
return [];

Check warning on line 44 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L42-L44

Added lines #L42 - L44 were not covered by tests
}

$streetNumber = array_pop($streetParts);
$street = implode(' ', $streetParts);

Check warning on line 48 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L47-L48

Added lines #L47 - L48 were not covered by tests

$postCode = $shippingAddress->getPostcode();
$city = $shippingAddress->getCity();

Check warning on line 51 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L51

Added line #L51 was not covered by tests
$countryCode = $shippingAddress->getCountryCode();
if (null === $street || null === $postCode || null === $countryCode) {
if (null === $postCode || null === $city || null === $countryCode) {

Check warning on line 53 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L53

Added line #L53 was not covered by tests
return [];
}

try {
$result = $this->client->get('/rest/businesslocation/v1/servicepoint/findNearestByAddress.json', [
'countryCode' => $countryCode,
'postalCode' => preg_replace('/\s+/', '', $postCode),
'streetName' => $street,
'numberOfServicePoints' => 10,
]);
$result = $this->client->servicePoints()->getNearestByAddress(NearestByAddressQuery::create(
streetName: $street,
streetNumber: $streetNumber,
postalCode: $postCode,
city: $city,
countryCode: $countryCode,
));

Check warning on line 64 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L58-L64

Added lines #L58 - L64 were not covered by tests
} catch (NetworkExceptionInterface $e) {
throw new TimeoutException($e);
}

$servicePoints = $result['servicePointInformationResponse']['servicePoints'] ?? [];
if (!is_array($servicePoints)) {
if ([] === $result->servicePoints) {

Check warning on line 69 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L69

Added line #L69 was not covered by tests
return [];
}

$pickupPoints = [];
foreach ($servicePoints as $servicePoint) {
foreach ($result->servicePoints as $servicePoint) {

Check warning on line 74 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L74

Added line #L74 was not covered by tests
$pickupPoints[] = $this->transform($servicePoint);
}

Expand All @@ -70,42 +81,25 @@
public function findPickupPoint(PickupPointCode $code): ?PickupPointInterface
{
try {
$result = $this->client->get('/rest/businesslocation/v1/servicepoint/findByServicePointId.json', [
'countryCode' => $code->getCountryPart(),
'servicePointId' => $code->getIdPart(),
]);
$result = $this->client->servicePoints()->getByIds(ByIdsQuery::create(
ids: [$code->getIdPart()],
countryCode: $code->getCountryPart(),
));

Check warning on line 87 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L84-L87

Added lines #L84 - L87 were not covered by tests
} catch (NetworkExceptionInterface $e) {
throw new TimeoutException($e);
}

$servicePoints = $result['servicePointInformationResponse']['servicePoints'] ?? null;
if (!is_array($servicePoints) || count($servicePoints) < 1) {
if ([] === $result->servicePoints) {

Check warning on line 92 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L92

Added line #L92 was not covered by tests
return null;
}

return $this->transform($servicePoints[0]);
return $this->transform($result->servicePoints[0]);

Check warning on line 96 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L96

Added line #L96 was not covered by tests
}

public function findAllPickupPoints(): iterable
{
try {
$result = $this->client->get('/rest/businesslocation/v1/servicepoint/getServicePointInformation.json');
} catch (NetworkExceptionInterface $e) {
throw new TimeoutException($e);
}

$servicePoints = $result['servicePointInformationResponse']['servicePoints'] ?? [];
if (!is_array($servicePoints)) {
return [];
}

foreach ($servicePoints as $servicePoint) {
if (!self::isValidServicePoint($servicePoint)) {
continue;
}

yield $this->transform($servicePoint);
}
// todo implement this
return [];

Check warning on line 102 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L102

Added line #L102 was not covered by tests
}

public function getCode(): string
Expand All @@ -118,51 +112,33 @@
return 'PostNord';
}

private function transform(array $servicePoint): PickupPointInterface
private function transform(ServicePoint $servicePoint): PickupPointInterface

Check warning on line 115 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L115

Added line #L115 was not covered by tests
{
$id = new PickupPointCode(
$servicePoint['servicePointId'],
$servicePoint->servicePointId,

Check warning on line 118 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L118

Added line #L118 was not covered by tests
$this->getCode(),
$servicePoint['visitingAddress']['countryCode'],
$servicePoint->visitingAddress->countryCode,

Check warning on line 120 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L120

Added line #L120 was not covered by tests
);

$address = '';

if (isset($servicePoint['visitingAddress']['streetName'])) {
$address .= $servicePoint['visitingAddress']['streetName'];
}

if (isset($servicePoint['visitingAddress']['streetNumber'])) {
$address .= ('' !== $address ? ' ' : '') . $servicePoint['visitingAddress']['streetNumber'];
}

$latitude = $longitude = null;
if (isset($servicePoint['coordinates'][0])) {
$latitude = (float) $servicePoint['coordinates'][0]['northing'];
$longitude = (float) $servicePoint['coordinates'][0]['easting'];
}

/** @var PickupPointInterface|object $pickupPoint */
$pickupPoint = $this->pickupPointFactory->createNew();

Assert::isInstanceOf($pickupPoint, PickupPointInterface::class);

$pickupPoint->setCode($id);
$pickupPoint->setName($servicePoint['name']);
$pickupPoint->setAddress($address);
$pickupPoint->setZipCode((string) $servicePoint['visitingAddress']['postalCode']);
$pickupPoint->setCity($servicePoint['visitingAddress']['city']);
$pickupPoint->setCountry((string) $servicePoint['visitingAddress']['countryCode']);
$pickupPoint->setLatitude($latitude);
$pickupPoint->setLongitude($longitude);
$pickupPoint->setName($servicePoint->name);
$pickupPoint->setAddress($servicePoint->visitingAddress->streetName . ' ' . $servicePoint->visitingAddress->streetNumber);
$pickupPoint->setZipCode($servicePoint->visitingAddress->postalCode);
$pickupPoint->setCity($servicePoint->visitingAddress->city);
$pickupPoint->setCountry($servicePoint->visitingAddress->countryCode);

Check warning on line 133 in src/Provider/PostNordProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Provider/PostNordProvider.php#L129-L133

Added lines #L129 - L133 were not covered by tests

return $pickupPoint;
}

private static function isValidServicePoint(array $servicePoint): bool
{
// some service points will not have a city because they are special internal service points
// we exclude these service points since they doesn't make any sense for the end user
// we exclude these service points since they don't make any sense for the end user
if (!isset($servicePoint['visitingAddress']['city'])) {
return false;
}
Expand Down
3 changes: 0 additions & 3 deletions src/Resources/config/app/fixtures.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ sylius_fixtures:
- 'PT'
- 'ES'
- 'CN'
- 'UK'
zones:
WORLD:
name: 'World'
Expand All @@ -38,7 +37,6 @@ sylius_fixtures:
- 'PT'
- 'ES'
- 'CN'
- 'UK'
DAO_PP_COUNTRIES:
name: 'Denmark'
countries:
Expand All @@ -60,7 +58,6 @@ sylius_fixtures:
- 'DE'
- 'PT'
- 'ES'
- 'UK'

setono_sylius_pickup_point_shipping_method:
options:
Expand Down
2 changes: 1 addition & 1 deletion tests/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Setono\SyliusPickupPointPlugin\Tests\Application;

use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;

final class Kernel extends BaseKernel
{
Expand Down
3 changes: 0 additions & 3 deletions tests/Application/config/routes/sylius_admin_api.yaml

This file was deleted.

Empty file.
Loading