Skip to content

Commit

Permalink
Merge pull request #3 from answear/data-fetching
Browse files Browse the repository at this point in the history
Data fetching
  • Loading branch information
malarzm authored Apr 19, 2021
2 parents a76544d + cfaa4df commit 0b041fb
Show file tree
Hide file tree
Showing 26 changed files with 1,192 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": "^7.4 || ^8.0",
"ext-xml": "*",
"ext-simplexml": "*",
"guzzlehttp/guzzle": "^6.0",
"marc-mabe/php-enum": "^4.3",
"symfony/http-kernel": "^4.4 || ^5.0",
Expand Down
12 changes: 12 additions & 0 deletions src/DependencyInjection/AnswearDpdPlPickupServicesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,17 @@ public function load(array $configs, ContainerBuilder $container): void
new FileLocator(__DIR__ . '/../Resources/config')
);
$loader->load('services.yaml');

$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

$definition = $container->getDefinition(ConfigProvider::class);
$definition->setArguments(
[
$config['url'],
$config['key'],
$config['requestTimeout'],
]
);
}
}
4 changes: 1 addition & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

class Configuration implements ConfigurationInterface
{
private const API_URL = 'https://mypudo.dpd.com.pl/api/';
private const CONNECTION_TIMEOUT = 10.0;
public const API_URL = 'https://mypudo.dpd.com.pl/api/pudo/';
private const REQUEST_TIMEOUT = 10.0;

public function getConfigTreeBuilder(): TreeBuilder
Expand All @@ -21,7 +20,6 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->scalarNode('url')->defaultValue(self::API_URL)->end()
->scalarNode('key')->cannotBeEmpty()->end()
->floatNode('connectionTimeout')->defaultValue(self::CONNECTION_TIMEOUT)->end()
->floatNode('requestTimeout')->defaultValue(self::REQUEST_TIMEOUT)->end()
->end();

Expand Down
53 changes: 53 additions & 0 deletions src/Enum/Day.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Answear\DpdPlPickupServicesBundle\Enum;

use MabeEnum\Enum;

class Day extends Enum
{
public const MONDAY = '1';
public const TUESDAY = '2';
public const WEDNESDAY = '3';
public const THURSDAY = '4';
public const FRIDAY = '5';
public const SATURDAY = '6';
public const SUNDAY = '7';

public static function monday(): self
{
return static::get(static::MONDAY);
}

public static function tuesday(): self
{
return static::get(static::TUESDAY);
}

public static function wednesday(): self
{
return static::get(static::WEDNESDAY);
}

public static function thursday(): self
{
return static::get(static::THURSDAY);
}

public static function friday(): self
{
return static::get(static::FRIDAY);
}

public static function saturday(): self
{
return static::get(static::SATURDAY);
}

public static function sunday(): self
{
return static::get(static::SUNDAY);
}
}
65 changes: 65 additions & 0 deletions src/Enum/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Answear\DpdPlPickupServicesBundle\Enum;

use MabeEnum\Enum;

class Service extends Enum
{
public const DELIVERY = '100';
public const COD = '101';
public const SWAP = '300';
public const DROPOFF_ONLINE = '200';
public const DROPOFF_OFFLINE = '201';
public const MONITORED_DOCUMENTS = 'P10';
public const ROD = 'P20';
public const TYRES = 'P30';
public const PALLET = 'P90';

public static function delivery(): self
{
return static::get(static::DELIVERY);
}

public static function cod(): self
{
return static::get(static::COD);
}

public static function swap(): self
{
return static::get(static::SWAP);
}

public static function dropoffOnline(): self
{
return static::get(static::DROPOFF_ONLINE);
}

public static function dropoffOffline(): self
{
return static::get(static::DROPOFF_OFFLINE);
}

public static function monitoredDocuments(): self
{
return static::get(static::MONITORED_DOCUMENTS);
}

public static function rod(): self
{
return static::get(static::ROD);
}

public static function tyres(): self
{
return static::get(static::TYRES);
}

public static function pallet(): self
{
return static::get(static::PALLET);
}
}
53 changes: 53 additions & 0 deletions src/Enum/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Answear\DpdPlPickupServicesBundle\Enum;

use MabeEnum\Enum;

class Type extends Enum
{
public const STANDARD = '100';
public const CHAIN = '200';
public const DPD = '300';
public const SWIP_BOX_1 = '400401';
public const SWIP_BOX_2 = '400402';
public const POINT_PACK = '500501';
public const FOREIGN_LOCKER = '400';

public static function standard(): self
{
return self::get(self::STANDARD);
}

public static function chain(): self
{
return self::get(self::CHAIN);
}

public static function dpd(): self
{
return self::get(self::DPD);
}

public static function swipBox1(): self
{
return self::get(self::SWIP_BOX_1);
}

public static function swipBox2(): self
{
return self::get(self::SWIP_BOX_2);
}

public static function pointPack(): self
{
return self::get(self::POINT_PACK);
}

public static function foreignLocker(): self
{
return self::get(self::FOREIGN_LOCKER);
}
}
21 changes: 21 additions & 0 deletions src/Exception/MalformedResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Answear\DpdPlPickupServicesBundle\Exception;

class MalformedResponseException extends \RuntimeException
{
private string $response;

public function __construct(string $response)
{
parent::__construct('Response is not a valid XML');
$this->response = $response;
}

public function getResponse(): string
{
return $this->response;
}
}
9 changes: 9 additions & 0 deletions src/Exception/ServiceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Answear\DpdPlPickupServicesBundle\Exception;

class ServiceException extends \RuntimeException
{
}
2 changes: 2 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ services:
autoconfigure: true
public: false

Answear\DpdPlPickupServicesBundle\:
resource: '../../{Service}'
34 changes: 34 additions & 0 deletions src/Service/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Answear\DpdPlPickupServicesBundle\Service;

class ConfigProvider
{
private string $url;
private string $key;
private float $requestTimeout;

public function __construct(string $url, string $key, float $requestTimeout)
{
$this->url = rtrim($url, '/') . '/';
$this->key = $key;
$this->requestTimeout = $requestTimeout;
}

public function getUrl(): string
{
return $this->url;
}

public function getKey(): string
{
return $this->key;
}

public function getRequestTimeout(): float
{
return $this->requestTimeout;
}
}
91 changes: 91 additions & 0 deletions src/Service/PUDOFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Answear\DpdPlPickupServicesBundle\Service;

use Answear\DpdPlPickupServicesBundle\Enum\Day;
use Answear\DpdPlPickupServicesBundle\Enum\Service;
use Answear\DpdPlPickupServicesBundle\Enum\Type;
use Answear\DpdPlPickupServicesBundle\ValueObject\AdditionalInfo;
use Answear\DpdPlPickupServicesBundle\ValueObject\Address;
use Answear\DpdPlPickupServicesBundle\ValueObject\Coordinates;
use Answear\DpdPlPickupServicesBundle\ValueObject\HolidayDates;
use Answear\DpdPlPickupServicesBundle\ValueObject\OpeningTime;
use Answear\DpdPlPickupServicesBundle\ValueObject\PUDO;
use Answear\DpdPlPickupServicesBundle\ValueObject\WeekStoreHours;

class PUDOFactory
{
public function fromXmlElement(\SimpleXMLElement $xml): PUDO
{
$pudo = new PUDO();
$pudo->active = 'true' === (string) $xml['active'];
$pudo->id = (string) $xml->PUDO_ID;
$pudo->type = Type::byValue((string) $xml->PUDO_TYPE);
$pudo->language = (string) $xml->LANGUAGE;
$pudo->address = $this->createAddress($xml);
$pudo->coordinates = new Coordinates((float) $xml->LATITUDE, (float) $xml->LONGITUDE);
$pudo->additionalInfo = $this->createAdditionalInfo($xml);
$pudo->opened = $this->createOpeningTimes($xml);
$pudo->holidays = $this->createHolidays($xml);

return $pudo;
}

private function createAddress(\SimpleXMLElement $xml): Address
{
$address = new Address();
$address->address1 = (string) $xml->ADDRESS1;
$address->address2 = (string) $xml->ADDRESS2;
$address->address3 = (string) $xml->ADDRESS3;
$address->locationHint = (string) $xml->LOCATION_HINT;
$address->zipCode = (string) $xml->ZIPCODE;
$address->city = (string) $xml->CITY;
$address->country = (string) $xml->COUNTRY;

return $address;
}

private function createAdditionalInfo(\SimpleXMLElement $xml): AdditionalInfo
{
$info = new AdditionalInfo();
foreach (explode(';', (string) $xml->SERVICE_PUDO) as $service) {
$info->services[] = Service::byValue($service);
}
$info->wheelchairAccessible = 'true' === (string) $xml->HANDICAPE;
$info->parking = 'true' === (string) $xml->PARKING;

return $info;
}

private function createOpeningTimes(\SimpleXMLElement $xml): WeekStoreHours
{
$openings = [];
foreach ($xml->OPENING_HOURS_ITEMS->OPENING_HOURS_ITEM as $opening) {
$openings[] = new OpeningTime(
Day::byValue((string) $opening->DAY_ID),
(string) $opening->START_TM,
(string) $opening->END_TM
);
}

return new WeekStoreHours(...$openings);
}

/**
* @return HolidayDates[]
*/
private function createHolidays(\SimpleXMLElement $xml): array
{
$dates = [];
foreach ($xml->HOLIDAY_ITEMS->HOLIDAY_ITEM as $holiday) {
$dates[] = new HolidayDates(
\DateTimeImmutable::createFromFormat('d/m/Y', (string) $holiday->START_TM)->setTime(0, 0, 0),
\DateTimeImmutable::createFromFormat('d/m/Y', (string) $holiday->END_TM)->setTime(23, 59, 59)
);
}

return $dates;
}
}
Loading

0 comments on commit 0b041fb

Please sign in to comment.