<?php declare(strict_types=1);
namespace BjerregaardTheme\Subscriber;
use DateTime;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Throwable;
class CheckoutConfirmPageSubscriber implements EventSubscriberInterface
{
private string $publicDir;
public function __construct(string $publicDir)
{
$this->publicDir = $publicDir;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageSubscriber',
];
}
public function onCheckoutConfirmPageSubscriber(CheckoutConfirmPageLoadedEvent $event): void
{
$dates = [];
$file = $this->publicDir . "/shipping.json";
if (!file_exists($file)) {
return;
}
try {
$dates = json_decode(file_get_contents($file));
$dates = array_filter($dates, fn($date) => isset($date->Calendar) && $date->Calendar === 'DANMARK');
array_walk($dates, fn(&$date) => $date = (new DateTime($date->Date))->format('Y-n-j'));
} catch (Throwable) {
}
$event->getPage()->addArrayExtension('bjerregaard', [
'disabledDates' => array_unique($dates),
]);
}
}