Skip to content

Commit

Permalink
Support for Italy PickupPoints
Browse files Browse the repository at this point in the history
  • Loading branch information
yekovalenkoa committed Feb 2, 2024
1 parent 86ff34b commit cd6baf3
Show file tree
Hide file tree
Showing 15 changed files with 401 additions and 98 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"ext-json": "*",
"guzzlehttp/guzzle": "^6.0",
"phpstan/phpstan-deprecation-rules": "^1.1",
"symfony/http-kernel": "^6.0",
"symfony/serializer": "^6.0",
"symfony/http-kernel": "^6.1",
"symfony/property-info": "^6.1",
"symfony/serializer": "^6.1",
"symfony/serializer-pack": "^1.3",
"webmozart/assert": "^1.11"
},
"require-dev": {
Expand All @@ -19,7 +21,7 @@
"phpstan/phpstan-webmozart-assert": "^1.2.4",
"phpunit/phpunit": "^10.5",
"roave/security-advisories": "dev-master",
"symfony/phpunit-bridge": "6.1.*"
"symfony/phpunit-bridge": "6.2.*"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 8 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ parameters:
treatPhpDocTypesAsCertain: false
paths:
- %rootDir%/../../../src
excludePaths:
- tests/*
ignoreErrors:
-
message: '#.*NodeDefinition::children.*#'
path: ./src/DependencyInjection

-
message: '#.*Extension::processConfiguration.*#'
path: ./src/DependencyInjection
3 changes: 2 additions & 1 deletion src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class Client
private ClientInterface $client;

public function __construct(
private readonly ConfigProvider $configProvider,
?ClientInterface $client = null,
) {
$this->client = $client ?? new GuzzleClient(
[
'base_uri' => ConfigProvider::BASE_URL,
'base_uri' => $this->configProvider->baseUrl,
]
);
}
Expand Down
9 changes: 6 additions & 3 deletions src/Command/FindPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ class FindPoints extends AbstractCommand
private Client $client;
private Serializer $serializer;

public function __construct(Client $client, Serializer $serializer)
{
public function __construct(
private readonly ConfigProvider $configProvider,
Client $client,
Serializer $serializer,
) {
$this->client = $client;
$this->serializer = $serializer;
}
Expand All @@ -27,7 +30,7 @@ public function findPoints(FindPointsRequest $request): FindPointsResponse
{
$httpRequest = new HttpRequest(
$request->getMethod(),
new Uri(ConfigProvider::API_VERSION . $request->getRequestUrl()),
new Uri($this->configProvider->apiVersion . $request->getRequestUrl()),
[
'Content-type' => 'application/json',
],
Expand Down
10 changes: 8 additions & 2 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

class ConfigProvider
{
public const BASE_URL = 'https://api-shipx-pl.easypack24.net/';
public const API_VERSION = 'v1';
private const BASE_URL = 'https://api-shipx-pl.easypack24.net/';
private const API_VERSION = 'v1';

public function __construct(
public readonly string $baseUrl = self::BASE_URL,
public readonly string $apiVersion = self::API_VERSION,
) {
}
}
33 changes: 32 additions & 1 deletion src/DependencyInjection/AnswearInpostExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,50 @@

namespace Answear\InpostBundle\DependencyInjection;

use Answear\InpostBundle\ConfigProvider;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class AnswearInpostExtension extends Extension
class AnswearInpostExtension extends Extension implements PrependExtensionInterface
{
private array $config;

public function prepend(ContainerBuilder $container): void
{
$configs = $container->getExtensionConfig($this->getAlias());
$this->setConfig($container, $configs);
}

/**
* @throws \Exception
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../Resources/config')
);
$loader->load('services.yaml');

$this->setConfig($container, $configs);

$definition = $container->getDefinition(ConfigProvider::class);
$definition->setArguments([
$this->config['baseUrl'],
$this->config['apiVersion'],
]);
}

private function setConfig(ContainerBuilder $container, array $configs): void
{
if (isset($this->config)) {
return;
}

$configuration = $this->getConfiguration($configs, $container);
$this->config = $this->processConfiguration($configuration, $configs);
}
}
24 changes: 24 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Answear\InpostBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('answear_inpost');

$treeBuilder->getRootNode()
->children()
?->scalarNode('baseUrl')->defaultNull()->end()
?->scalarNode('apiVersion')->defaultNull()->end()
?->end();

return $treeBuilder;
}
}
1 change: 1 addition & 0 deletions src/Enum/PointType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ enum PointType: string
case Pop = 'pop';
case ParcelLockerOnly = 'parcel_locker_only';
case ParcelLockerSuperpop = 'parcel_locker_superpop';
case Pok = 'pok';
}
3 changes: 3 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ services:
autoconfigure: true
public: false

Answear\InpostBundle\ConfigProvider: ~
Answear\InpostBundle\Client: ~

Answear\InpostBundle\:
resource: '../../../src/{Command,Client}'

12 changes: 9 additions & 3 deletions src/Response/Struct/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Item
public ?string $locationDescription2;
public ?int $distance;
public ?string $openingHours;
public ?ItemOperatingHours $operatingHoursExtended;
public ?ItemAddress $address;
public ?ItemAddressDetails $addressDetails;
public ?string $phoneNumber;
Expand All @@ -36,6 +37,7 @@ class Item
/** @var string[] */
public ?array $recommendedLowInterestBoxMachinesList;
public ?bool $location247;
public ?bool $easyAccessZone;

public static function fromArray(array $pointData): self
{
Expand All @@ -53,18 +55,22 @@ public static function fromArray(array $pointData): self
$point->locationDescription2 = $pointData['location_description_2'] ?? null;
$point->distance = $pointData['distance'] ?? null;
$point->openingHours = $pointData['opening_hours'] ?? null;
$point->operatingHoursExtended = !empty($pointData['operating_hours_extended'])
? ItemOperatingHours::fromArray($pointData['operating_hours_extended'])
: null;
$point->address = !empty($pointData['address']) ? ItemAddress::fromArray($pointData['address']) : null;
$point->addressDetails = !empty($pointData['address_details']) ? ItemAddressDetails::fromArray($pointData['address_details']) : null;
$point->phoneNumber = $pointData['phone_number'] ?? null;
$point->paymentPointDescr = $pointData['payment_point_descr'] ?? null;
$point->functions = !empty($pointData['functions']) ? self::getFunctionsTypes($pointData['functions']) : null;
$point->partnerId = $pointData['partner_id'] ?? null;
$point->isNext = $pointData['is_next'] ?? null;
$point->paymentAvailable = $pointData['payment_available'] ?? null;
$point->isNext = $pointData['is_next'];
$point->paymentAvailable = $pointData['payment_available'];
$point->paymentType = $pointData['payment_type'] ?? null;
$point->virtual = $pointData['virtual'] ?? null;
$point->recommendedLowInterestBoxMachinesList = $pointData['recommended_low_interest_box_machines_list'] ?? null;
$point->location247 = $pointData['location_247'] ?? null;
$point->location247 = $pointData['location_247'];
$point->easyAccessZone = $pointData['easy_access_zone'];

return $point;
}
Expand Down
37 changes: 37 additions & 0 deletions src/Response/Struct/ItemOperatingHours.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Answear\InpostBundle\Response\Struct;

class ItemOperatingHours
{
public array $sunday = [];
public array $monday = [];
public array $tuesday = [];
public array $wednesday = [];
public array $thursday = [];
public array $friday = [];
public array $saturday = [];

public static function fromArray(array $operatingHours): self
{
$self = new self();

if (!isset($operatingHours['customer'])) {
return $self;
}

if (!is_array($operatingHours['customer'])) {
return $self;
}

foreach ($operatingHours['customer'] as $day => $hours) {
if (isset($self->{strtolower($day)})) {
$self->{strtolower($day)} = $hours;
}
}

return $self;
}
}
Loading

0 comments on commit cd6baf3

Please sign in to comment.