generated from Setono/SyliusPluginSkeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
531 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusShipmondoPlugin\Controller\Shop; | ||
|
||
use Setono\Shipmondo\Client\ClientInterface; | ||
use Setono\Shipmondo\Request\PickupPoints\PickupPointsCollectionQuery; | ||
use Setono\Shipmondo\Response\PickupPoints\PickupPoint; | ||
use Setono\SyliusShipmondoPlugin\Model\OrderInterface; | ||
use Setono\SyliusShipmondoPlugin\Model\ShipmentInterface; | ||
use Setono\SyliusShipmondoPlugin\Model\ShippingMethodInterface; | ||
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface; | ||
use Sylius\Component\Order\Context\CartContextInterface; | ||
use Sylius\Component\Order\Context\CartNotFoundException; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Twig\Environment; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class GetPickupPointsAction | ||
{ | ||
public function __construct( | ||
private readonly ShippingMethodRepositoryInterface $shippingMethodRepository, | ||
private readonly CartContextInterface $cartContext, | ||
private readonly ClientInterface $client, | ||
private readonly Environment $twig, | ||
) { | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
$shippingMethodCode = $request->query->getString('shippingMethod'); | ||
|
||
if ('' === $shippingMethodCode) { | ||
return new JsonResponse(status: Response::HTTP_BAD_REQUEST); | ||
} | ||
|
||
$shippingMethod = $this->shippingMethodRepository->findOneBy([ | ||
'code' => $shippingMethodCode, | ||
'enabled' => true, | ||
]); | ||
|
||
if (!$shippingMethod instanceof ShippingMethodInterface) { | ||
return new JsonResponse(status: Response::HTTP_BAD_REQUEST); | ||
} | ||
|
||
try { | ||
$order = $this->cartContext->getCart(); | ||
} catch (CartNotFoundException) { | ||
return new JsonResponse(status: Response::HTTP_BAD_REQUEST); | ||
} | ||
|
||
if (!$order instanceof OrderInterface) { | ||
return new JsonResponse(status: Response::HTTP_BAD_REQUEST); | ||
} | ||
|
||
$shippingAddress = $order->getShippingAddress(); | ||
if (null === $shippingAddress) { | ||
return new JsonResponse(status: Response::HTTP_BAD_REQUEST); | ||
} | ||
|
||
try { | ||
$pickupPoints = $this->client->pickupPoints()->get(new PickupPointsCollectionQuery( | ||
carrierCode: (string) $shippingMethod->getCarrierCode(), | ||
countryCode: (string) $shippingAddress->getCountryCode(), | ||
zipCode: (string) $shippingAddress->getPostcode(), | ||
address: (string) $shippingAddress->getStreet(), | ||
))->items; | ||
} catch (\InvalidArgumentException) { | ||
return new JsonResponse(status: Response::HTTP_BAD_REQUEST); | ||
} | ||
|
||
// The customer might already have chosen a pickup point, | ||
// so we need to check if the shipment has a chosen pickup point and put that pickup point at the top of the list | ||
$chosenPickupPoint = self::resolveChosenPickupPoint($order); | ||
|
||
usort($pickupPoints, static function (PickupPoint $a, PickupPoint $b) use ($chosenPickupPoint) { | ||
if ($a->id === $chosenPickupPoint) { | ||
return -1; | ||
} | ||
|
||
if ($b->id === $chosenPickupPoint) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
}); | ||
|
||
return new JsonResponse([ | ||
'pickupPoints' => $pickupPoints, | ||
'html' => $this->twig->render('@SetonoSyliusShipmondoPlugin/shop/ajax/pickup_points.html.twig', [ | ||
'pickupPoints' => $pickupPoints, | ||
]), | ||
]); | ||
} | ||
|
||
/** | ||
* Will return the id of the chosen pickup point if the shipment has one | ||
* | ||
* todo this only works if the order has _one_ shipment | ||
*/ | ||
private static function resolveChosenPickupPoint(OrderInterface $order): ?string | ||
{ | ||
$shipment = $order->getShipments()->first(); | ||
if (!$shipment instanceof ShipmentInterface) { | ||
return null; | ||
} | ||
|
||
$pickupPoint = $shipment->getShipmondoPickupPoint(); | ||
if (null === $pickupPoint) { | ||
return null; | ||
} | ||
|
||
$id = $pickupPoint['id'] ?? null; | ||
Assert::nullOrString($id); | ||
|
||
return $id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusShipmondoPlugin\Form\Extension; | ||
|
||
use Setono\SyliusShipmondoPlugin\Model\ShipmentInterface; | ||
use Sylius\Bundle\CoreBundle\Form\Type\Checkout\ShipmentType; | ||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\CallbackTransformer; | ||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
/** | ||
* @extends AbstractTypeExtension<ShipmentInterface> | ||
*/ | ||
final class ShipmentTypeExtension extends AbstractTypeExtension | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder->add('shipmondoPickupPoint', HiddenType::class); | ||
$builder->get('shipmondoPickupPoint')->addModelTransformer(new CallbackTransformer( | ||
function (?array $pickupPoint): ?string { | ||
if (null === $pickupPoint) { | ||
return null; | ||
} | ||
|
||
return json_encode($pickupPoint, \JSON_THROW_ON_ERROR); | ||
}, | ||
function (?string $json): ?array { | ||
if (null === $json) { | ||
return null; | ||
} | ||
|
||
$data = json_decode($json, true, 512, \JSON_THROW_ON_ERROR); | ||
|
||
if (!is_array($data)) { | ||
return null; | ||
} | ||
|
||
return $data; | ||
}, | ||
)); | ||
} | ||
|
||
public static function getExtendedTypes(): iterable | ||
{ | ||
yield ShipmentType::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusShipmondoPlugin\Model; | ||
|
||
use Sylius\Component\Core\Model\ShipmentInterface as BaseShipmentInterface; | ||
|
||
interface ShipmentInterface extends BaseShipmentInterface | ||
{ | ||
public function setShipmondoPickupPoint(?array $shipmondoPickupPoint): void; | ||
|
||
public function getShipmondoPickupPoint(): ?array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusShipmondoPlugin\Model; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
trait ShipmentTrait | ||
{ | ||
/** @ORM\Column(type="json", nullable=true) */ | ||
protected ?array $shipmondoPickupPoint = null; | ||
|
||
public function setShipmondoPickupPoint(?array $shipmondoPickupPoint): void | ||
{ | ||
$this->shipmondoPickupPoint = $shipmondoPickupPoint; | ||
} | ||
|
||
public function getShipmondoPickupPoint(): ?array | ||
{ | ||
return $this->shipmondoPickupPoint; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
setono_sylius_shipmondo_global: | ||
resource: "@SetonoSyliusShipmondoPlugin/Resources/config/routes/global.yaml" | ||
|
||
setono_sylius_shipmondo_shop: | ||
resource: "@SetonoSyliusShipmondoPlugin/Resources/config/routes/shop.yaml" | ||
prefix: /{_locale} | ||
requirements: | ||
_locale: ^[A-Za-z]{2,4}(_([A-Za-z]{4}|[0-9]{3}))?(_([A-Za-z]{2}|[0-9]{3}))?$ | ||
|
||
setono_sylius_shipmondo_admin: | ||
resource: "@SetonoSyliusShipmondoPlugin/Resources/config/routes/admin.yaml" | ||
prefix: /admin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
setono_sylius_shipmondo_shop_ajax_get_pickup_points: | ||
path: /ajax/get-pickup-points | ||
methods: [GET] | ||
defaults: | ||
_controller: setono_sylius_shipmondo.controller.shop.get_pickup_points |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="setono_sylius_shipmondo.twig.extension" | ||
class="Setono\SyliusShipmondoPlugin\Twig\Extension"> | ||
<tag name="twig.extension"/> | ||
</service> | ||
|
||
<service id="setono_sylius_shipmondo.twig.runtime" | ||
class="Setono\SyliusShipmondoPlugin\Twig\Runtime"> | ||
<argument type="service" id="sylius.repository.shipping_method"/> | ||
|
||
<tag name="twig.runtime"/> | ||
</service> | ||
</services> | ||
</container> |
Oops, something went wrong.