Skip to content

Commit

Permalink
add Ecs
Browse files Browse the repository at this point in the history
  • Loading branch information
ehibes committed Apr 1, 2023
1 parent 34d61e5 commit a58a348
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 122 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ jobs:
name: Run Psalm
run: vendor/bin/psalm

-
name: Run ECS
run: vendor/bin/ecs check src

-
name: Run PHPSpec
run: vendor/bin/phpspec run --ansi -f progress --no-interaction
Expand Down
1 change: 0 additions & 1 deletion features/check_city_postcode_match.feature
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ Feature: Checking postcode and zipcode match
And I specify billing country as "France"
And I specify billing postcode as "64200"
And I try to complete the addressing step
#Then I should be on the checkout addressing step
Then I should be notified that the city does not match the postcode
19 changes: 6 additions & 13 deletions src/Action/CityChoicesByZipCodeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@

namespace Waaz\SyliusTntPlugin\Action;

use TNTExpress\Model\City;
use FOS\RestBundle\View\View;
use TNTExpress\Client\TNTClientInterface;
use TNTExpress\Exception\ClientException;
use FOS\RestBundle\View\ViewHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Form\DataTransformerInterface;
use Setono\SyliusPickupPointPlugin\Model\PickupPoint;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use TNTExpress\Client\TNTClientInterface;
use TNTExpress\Exception\ClientException;
use TNTExpress\Model\City;

final class CityChoicesByZipCodeAction
{
public function __construct(
private TNTClientInterface $tntClient
private TNTClientInterface $tntClient,
) {
}

Expand All @@ -28,11 +22,10 @@ public function __invoke(string $postcode): JsonResponse
if ('' === $postcode) {
throw new NotFoundHttpException();
}

try {
$result = $this->tntClient->getCitiesGuide($postcode);
$cities = array_map(fn(City $city): string => ucwords(strtolower($city->getName() ?? '')), $result);

$cities = array_map(fn (City $city): string => ucwords(strtolower($city->getName() ?? '')), $result);
} catch (ClientException $e) {
$cities = [];
}
Expand Down
51 changes: 23 additions & 28 deletions src/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@

namespace Waaz\SyliusTntPlugin\Api;

use TNTExpress\Model\Sender;
use Webmozart\Assert\Assert;
use TNTExpress\Model\Address;
use TNTExpress\Model\Service;
use TNTExpress\Model\Receiver;
use TNTExpress\Client\TNTClient;
use TNTExpress\Model\Expedition;
use TNTExpress\Model\ParcelRequest;
use TNTExpress\Model\ExpeditionRequest;
use TNTExpress\Client\SoapClientBuilder;
use TNTExpress\Exception\ExceptionManager;
use Sylius\Component\Core\Model\OrderInterface;
use BitBag\SyliusShippingExportPlugin\Entity\ShippingGatewayInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use TNTExpress\Exception\ExceptionManagerInterface;
use TNTExpress\Client\TNTClient;
use TNTExpress\Exception\InvalidPairZipcodeCityException;
use BitBag\SyliusShippingExportPlugin\Entity\ShippingGatewayInterface;
use TNTExpress\Model\Address;
use TNTExpress\Model\Expedition;
use TNTExpress\Model\ExpeditionRequest;
use TNTExpress\Model\ParcelRequest;
use TNTExpress\Model\Receiver;
use TNTExpress\Model\Sender;
use TNTExpress\Model\Service;
use Webmozart\Assert\Assert;

class Client implements ClientInterface
{
Expand Down Expand Up @@ -55,7 +52,7 @@ public function createExpedition(): Expedition
$feasibility = $this->getFeasibility($expeditionRequest);

$expeditionRequest->setServiceCode($feasibility->getServiceCode());

return $this->tntClient->createExpedition($expeditionRequest);
}

Expand All @@ -76,7 +73,7 @@ private function createSender(): Sender
->setFaxNumber($this->shippingGateway->getConfigValue('sender_fax_number'))
->setType($this->shippingGateway->getConfigValue('sender_type'))
;

return $sender;
}

Expand All @@ -87,15 +84,15 @@ private function createReceiver(): Receiver
Assert::isInstanceOf($this->shipment, ShipmentInterface::class, '$shipment must be set before expedition creation.');
Assert::isInstanceOf($this->shippingGateway, ShippingGatewayInterface::class, '$shippingGateway must be set before expedition creation.');

/** @var OrderInterface */
/** @var OrderInterface $order */
$order = $this->shipment->getOrder();

/** @var AddressInterface */
/** @var AddressInterface $address */
$address = $order->getShippingAddress();

/** @var CustomerInterface */
/** @var CustomerInterface $customer */
$customer = $order->getCustomer();

$receiver->setContactFirstName($address->getFirstName())
->setContactLastName($address->getLastName())
->setPhoneNumber($address->getPhoneNumber())
Expand All @@ -112,19 +109,19 @@ private function createReceiver(): Receiver

private function createParcelRequest(): ParcelRequest
{
$parcelRequest = new ParcelRequest;
$parcelRequest = new ParcelRequest();
$parcelRequest->setSequenceNumber(1);

Assert::isInstanceOf($this->shipment, ShipmentInterface::class, '$shipment must be set before expedition creation.');

// get bundle config value for weightUnit
$weight = $this->shipment->getShippingWeight()/1000;
$weight = $this->shipment->getShippingWeight() / 1000;

if ($this->weightUnit === 'g') {
$weight = $weight / 1000;
}

$parcelRequest->setWeight(sprintf("%.3f", $weight));
$parcelRequest->setWeight(sprintf('%.3f', $weight));

return $parcelRequest;
}
Expand All @@ -133,8 +130,7 @@ private function verifyAddresses(array $addresses): void
{
foreach ($addresses as $address) {
Assert::isInstanceOf($address, Address::class);
if (false === $this->tntClient->checkZipcodeCityMatch($address->getZipCode(), $address->getCity()))
{
if (false === $this->tntClient->checkZipcodeCityMatch($address->getZipCode(), $address->getCity())) {
throw new InvalidPairZipcodeCityException($address->getZipCode(), $address->getCity());
}
}
Expand All @@ -153,7 +149,6 @@ private function createExpeditionRequest(Sender $sender, Receiver $receiver, Par
$expeditionRequest->setLabelFormat($this->shippingGateway->getConfigValue('label_format'));

return $expeditionRequest;

}

/** Must be improved **/
Expand All @@ -172,4 +167,4 @@ private function getFeasibility(ExpeditionRequest $expeditionRequest): Service

return $service;
}
}
}
8 changes: 4 additions & 4 deletions src/Api/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace Waaz\SyliusTntPlugin\Api;

use TNTExpress\Model\Expedition;
use Sylius\Component\Core\Model\ShipmentInterface;
use BitBag\SyliusShippingExportPlugin\Entity\ShippingGatewayInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use TNTExpress\Model\Expedition;

interface ClientInterface
{
public function setShippingGateway(ShippingGatewayInterface $shippingGateway): void;

public function setShipment(ShipmentInterface $shipment): void;

public function createExpedition(): Expedition;
}
}
23 changes: 10 additions & 13 deletions src/Api/ShippingLabelFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,43 @@

namespace Waaz\SyliusTntPlugin\Api;

use Webmozart\Assert\Assert;
use TNTExpress\Model\Expedition;
use Waaz\SyliusTntPlugin\Api\Client;
use Sylius\Component\Order\Model\OrderInterface;
use BitBag\SyliusShippingExportPlugin\Entity\ShippingGatewayInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Order\Model\OrderInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use BitBag\SyliusShippingExportPlugin\Entity\ShippingGatewayInterface;
use TNTExpress\Model\Expedition;
use Webmozart\Assert\Assert;

class ShippingLabelFetcher implements ShippingLabelFetcherInterface
{
private ?Expedition $response = null;

public function __construct(
private FlashBagInterface $flashBag,
private Client $client
private Client $client,
) {
}

public function createShipment(ShippingGatewayInterface $shippingGateway, ShipmentInterface $shipment): void
{
try {
$this->client->setShippingGateway($shippingGateway);
$this->client->setShipment($shipment);
$this->response = $this->client->createExpedition();

} catch (\Exception $exception) {
$order = $shipment->getOrder();
Assert::isInstanceOf($order, OrderInterface::class);

/** @var string */
$number = $order->getNumber();

/** @var string $number */
$number = $order->getNumber();

$this->flashBag->add(
'error',
sprintf(
'TNT Service for #%s order: %s',
$number,
$exception->getMessage()
)
$exception->getMessage(),
),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Api/ShippingLabelFetcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Waaz\SyliusTntPlugin\Api;

use Sylius\Component\Core\Model\ShipmentInterface;
use BitBag\SyliusShippingExportPlugin\Entity\ShippingGatewayInterface;
use Sylius\Component\Core\Model\ShipmentInterface;

interface ShippingLabelFetcherInterface
{
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Waaz\SyliusTntPlugin\DependencyInjection;

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

final class Configuration implements ConfigurationInterface
{
Expand All @@ -22,7 +22,7 @@ public function getConfigTreeBuilder(): TreeBuilder
return $treeBuilder;
}

protected function addGlobalSection(ArrayNodeDefinition $node): void
private function addGlobalSection(ArrayNodeDefinition $node): void
{
$node
->children()
Expand All @@ -48,4 +48,4 @@ protected function addGlobalSection(ArrayNodeDefinition $node): void
->end()
;
}
}
}
6 changes: 3 additions & 3 deletions src/DependencyInjection/WaazSyliusTntExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Waaz\SyliusTntPlugin\DependencyInjection;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

final class WaazSyliusTntExtension extends Extension
Expand All @@ -19,7 +19,7 @@ public function load(array $configs, ContainerBuilder $container): void
{
$config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
foreach ($config as $key => $value) {
$container->setParameter('waaz_sylius_tnt_plugin.'.$key, $value);
$container->setParameter('waaz_sylius_tnt_plugin.' . $key, $value);
}

$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
Expand All @@ -31,4 +31,4 @@ public function getConfiguration(array $config, ContainerBuilder $container): Co
{
return new Configuration();
}
}
}
21 changes: 10 additions & 11 deletions src/EventListener/ShippingExportEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace Waaz\SyliusTntPlugin\EventListener;

use Webmozart\Assert\Assert;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Filesystem\Filesystem;
use Sylius\Component\Core\Model\ShipmentInterface;
use Waaz\SyliusTntPlugin\Api\ShippingLabelFetcherInterface;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use BitBag\SyliusShippingExportPlugin\Entity\ShippingExportInterface;
use BitBag\SyliusShippingExportPlugin\Repository\ShippingExportRepository;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Core\Model\ShipmentInterface;
use Symfony\Component\Filesystem\Filesystem;
use Waaz\SyliusTntPlugin\Api\ShippingLabelFetcherInterface;
use Webmozart\Assert\Assert;

class ShippingExportEventListener
{
Expand All @@ -21,7 +20,7 @@ public function __construct(
private Filesystem $filesystem,
private ShippingExportRepository $shippingExportRepository,
private string $shippingLabelsPath,
private ShippingLabelFetcherInterface $shippingLabelFetcher
private ShippingLabelFetcherInterface $shippingLabelFetcher,
) {
}

Expand Down Expand Up @@ -52,7 +51,7 @@ public function exportShipment(ResourceControllerEvent $event): void
public function saveShippingLabel(
ShippingExportInterface $shippingExport,
string $labelContent,
string $labelExtension
string $labelExtension,
): void {
$labelPath = $this->shippingLabelsPath
. '/' . $this->getFilename($shippingExport)
Expand All @@ -72,18 +71,18 @@ private function getFilename(ShippingExportInterface $shippingExport): string
$order = $shipment->getOrder();
Assert::notNull($order);

/** @var string */
/** @var string $orderNumber */
$orderNumber = $order->getNumber();

/** @var int */
/** @var int $shipmentId */
$shipmentId = $shipment->getId();

return implode(
'_',
[
$shipmentId,
preg_replace('~[^A-Za-z0-9]~', '', $orderNumber),
]
],
);
}

Expand Down
Loading

0 comments on commit a58a348

Please sign in to comment.