src/EventSubscriber/CustomerSubscriber.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\CustomerEvent;
  4. use App\Repository\UserRepository;
  5. use App\Security\Voters\InvoiceVoter;
  6. use App\Service\Constants\LogEvents;
  7. use App\Service\Invoice\Outgoing\OutgoingInvoiceService;
  8. use App\Service\Subscription\SubscriptionService;
  9. use App\Service\UserLog;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. class CustomerSubscriber implements EventSubscriberInterface
  13. {
  14.     protected Security $security;
  15.     protected UserRepository $userRepo;
  16.     protected SubscriptionService $subscriptionService;
  17.     private UserLog $userLog;
  18.     private OutgoingInvoiceService $outgoingInvoiceService;
  19.     public function __construct(
  20.         SubscriptionService    $subscriptionService,
  21.         OutgoingInvoiceService $outgoingInvoiceService,
  22.         Security               $security,
  23.         UserLog                $userLog
  24.     )
  25.     {
  26.         $this->security $security;
  27.         $this->subscriptionService $subscriptionService;
  28.         $this->userLog $userLog;
  29.         $this->outgoingInvoiceService $outgoingInvoiceService;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             CustomerEvent::INVOICE_DEFAULTS_UPDATED => [
  35.                 ['logInvoiceDefaultsChange'],
  36.                 ['updateCustomerJsonData']
  37.             ],
  38.             CustomerEvent::ADDRESS_DEFAULTS_UPDATED => [
  39.                 ['logAddressDefaultsChange'],
  40.                 ['updateCustomerJsonData']
  41.             ],
  42.             CustomerEvent::CUSTOMER_BASICS_UPDATED => [
  43.                 ['logCustomerBasicsChange'],
  44.                 ['updateCustomerJsonData']
  45.             ],
  46.             CustomerEvent::CUSTOMER_CREATED => [
  47.                 ['createSubscriptions']
  48.             ],
  49.         ];
  50.     }
  51.     public function updateCustomerJsonData(CustomerEvent $event)
  52.     {
  53.         $customer $event->getCustomer();
  54.         foreach ($customer->getInvoices() as $invoice) {
  55.             if (!$this->security->isGranted(InvoiceVoter::CAN_BE_UPDATED$invoice))
  56.             {
  57.                 continue;
  58.             }
  59.             $this->outgoingInvoiceService->updateCustomerJsonData($invoice);
  60.         }
  61.     }
  62.     public function logInvoiceDefaultsChange(CustomerEvent $event)
  63.     {
  64.         $this->userLog->log('customer',LogEvents::CUSTOMER_UPDATED_INVOICE_DEFAULTS_APP,
  65.             $event->getCustomer()->getId(),
  66.             $event->getCustomer()->getName()
  67.         );
  68.     }
  69.     public function logAddressDefaultsChange(CustomerEvent $event)
  70.     {
  71.         $this->userLog->log('customer',LogEvents::CUSTOMER_UPDATED_ADDRESS_DEFAULTS_APP,
  72.             $event->getCustomer()->getId(),
  73.             $event->getCustomer()->getName()
  74.         );
  75.     }
  76.     public function logCustomerBasicsChange(CustomerEvent $event)
  77.     {
  78.         $this->userLog->log('customer',LogEvents::CUSTOMER_UPDATED_BASICS_APP,
  79.             $event->getCustomer()->getId(),
  80.             $event->getCustomer()->getName()
  81.         );
  82.     }
  83.     public function createSubscriptions(CustomerEvent $event) {
  84.         if ($customer $event->getCustomer()) {
  85.             $this->subscriptionService->generateSubscriptions($customer);
  86.         }
  87.     }
  88. }