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

Added support for Italy parcel points #9

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
11 changes: 9 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ 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,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API version can be included in base url? Since we have base url in env, we can change api version dynamically.
What I mean is removing the API version env variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, it is better to keep the version as a variable and not put it in base_url

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason? :D
Take a look at the variables in env that end with _URL

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @olekans

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed :)

]
);
}
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()),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refers to the comment above ☝️ Then the API version does not need to be appended to every request. Am I right?

[
'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}'

6 changes: 6 additions & 0 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,6 +55,9 @@ 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;
Expand All @@ -65,6 +70,7 @@ public static function fromArray(array $pointData): self
$point->virtual = $pointData['virtual'] ?? null;
$point->recommendedLowInterestBoxMachinesList = $pointData['recommended_low_interest_box_machines_list'] ?? null;
$point->location247 = $pointData['location_247'] ?? null;
$point->easyAccessZone = $pointData['easy_access_zone'] ?? null;

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

declare(strict_types=1);

namespace Answear\InpostBundle\Response\Struct;

class ItemOperatingHours
{
/**
* @var ItemOperatingHoursDay[] $sunday
*/
public array $sunday = [];
/**
* @var ItemOperatingHoursDay[] $monday
*/
public array $monday = [];
/**
* @var ItemOperatingHoursDay[] $tuesday
*/
public array $tuesday = [];
/**
* @var ItemOperatingHoursDay[] $wednesday
*/
public array $wednesday = [];
/**
* @var ItemOperatingHoursDay[] $thursday
*/
public array $thursday = [];
/**
* @var ItemOperatingHoursDay[] $friday
*/
public array $friday = [];
/**
* @var ItemOperatingHoursDay[] $saturday
*/
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;
}
Comment on lines +42 to +48
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!isset($operatingHours['customer'])) {
return $self;
}
if (!is_array($operatingHours['customer'])) {
return $self;
}
if (!empty($operatingHours['customer'] ?? [])) {
return $self;
}

#whatever

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, the suggested code does not check whether a variable is an array


foreach ($operatingHours['customer'] as $day => $hours) {
if (!isset($self->{strtolower($day)})) {
continue;
}
foreach ($hours as $periodHours) {
$self->{strtolower($day)}[] = ItemOperatingHoursDay::fromArray($periodHours);
}
}

return $self;
}
}
38 changes: 38 additions & 0 deletions src/Response/Struct/ItemOperatingHoursDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Answear\InpostBundle\Response\Struct;

class ItemOperatingHoursDay
{
public function __construct(
public ?int $start = null,
public ?int $end = null,
public ?string $startParsed = null,
public ?string $endParsed = null,
) {
}

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

if (isset($day['start'])) {
$self->start = $day['start'];
$self->startParsed = self::parseTime($day['start']);
}

if (isset($day['end'])) {
$self->end = $day['end'];
$self->endParsed = self::parseTime($day['end']);
}

return $self;
}

private static function parseTime(int $timestamp): string
{
return (new \DateTimeImmutable())->setTimestamp($timestamp * 60)->format('H:i');
}
}
Loading
Loading