<?php
/**
* @author Thomas HERISSON (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\DhlBundle\EventSubscriber;
use Bluue\ShipmentsBundle\Repository\CarrierRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Bluue\ShipmentsBundle\Event\ConfigurationCarriersForEditOrderCarrierEvent;
use App\Services\Context as ContextService;
class ConfigureCarriersForEditOrderCarrierSubscriber implements EventSubscriberInterface
{
/**
* @var CarrierRepository $carrierRepo
*/
private CarrierRepository $carrierRepo;
/**
* @var ContextService $contextService
*/
private ContextService $contextService;
public function __construct(
CarrierRepository $carrierRepo,
ContextService $contextService
) {
$this->carrierRepo = $carrierRepo;
$this->contextService = $contextService;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
ConfigurationCarriersForEditOrderCarrierEvent::NAME => 'onLoad'
];
}
/**
* @param ConfigurationCarriersForEditOrderCarrierEvent $event
* @return void
*/
public function onLoad(ConfigurationCarriersForEditOrderCarrierEvent $event): void
{
$allCarriers = $event->getCarriers();
$order = $event->getOrder();
$context = $order ? $order->getContext() : $this->contextService->getActualOrDefault();
$carriers = $this->carrierRepo->createQueryBuilder('c')
->where('c.module = :module')->setParameter('module', 'dhlexpress')
->leftJoin('c.carrierContexts', 'carrierContexts')
->leftJoin('carrierContexts.context', 'context')
->andWhere('context.id = :context_id')
->setParameter('context_id', $context->getId()->toBinary());
foreach ($carriers->getQuery()->getResult() as $carrier) {
$allCarriers[] = $carrier;
}
$event->setCarriers($allCarriers);
}
}