<?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\SalesBundle\EventSubscriber;
use App\Services\Configuration;
use Bluue\SalesBundle\Event\ConfigurationAccessQuotationEditEvent;
use Bluue\SalesBundle\Security\SalesVoter;
use Symfony\Component\Security\Core\Security;
use Bluue\SalesBundle\Event\ConfigurationAccessOrderEditEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AccessEditDocumentValidatedSubscriber implements EventSubscriberInterface
{
/**
* @var Configuration
*/
protected Configuration $configuration;
/**
* @var Security
*/
protected Security $security;
/**
* @param Configuration $configuration
* @param Security $security
*/
public function __construct(Configuration $configuration, Security $security)
{
$this->configuration = $configuration;
$this->security = $security;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
ConfigurationAccessOrderEditEvent::NAME => 'orderEdit',
ConfigurationAccessQuotationEditEvent::NAME => 'quotationEdit'
];
}
/**
* @param ConfigurationAccessOrderEditEvent $event
* @return void
*/
public function orderEdit(ConfigurationAccessOrderEditEvent $event): void
{
$order = $event->getOrder();
if (
!$this->configuration->get('sales_bundle__modify_quotations_and_orders_validated')
|| !$this->security->isGranted(SalesVoter::ACCESS, $order)
|| !$order->getInvoices()->isEmpty()
|| !$order->getFilterDeliveryNotes(false, false, false, true)->isEmpty()
) {
return;
}
$event->setIsAccess(true);
}
/**
* @param ConfigurationAccessQuotationEditEvent $event
* @return void
*/
public function quotationEdit(ConfigurationAccessQuotationEditEvent $event): void
{
$quotation = $event->getQuotation();
if (
!$this->configuration->get('sales_bundle__modify_quotations_and_orders_validated')
|| !$this->security->isGranted(SalesVoter::ACCESS, $quotation)
|| $quotation->getOrder()
|| !$quotation->getInvoices()->isEmpty()
) {
return;
}
$event->setAccess(true);
}
}