src/Services/Transaction/Workflow/EventSubscriber/Internal/UpdateCreditorsJsonSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Services\Transaction\Workflow\EventSubscriber\Internal;
  3. use App\Entity\Client;
  4. use App\Entity\Transaction;
  5. use Doctrine\ORM\EntityManager;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Workflow\Event\Event;
  9. class UpdateCreditorsJsonSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var EntityManager
  13.      */
  14.     private $entityManager;
  15.     public function __construct(EntityManagerInterface $entityManager)
  16.     {
  17.         /** @var EntityManager $em */
  18.         $em $entityManager;
  19.         $this->entityManager $em;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             'workflow.internal_transaction_creating.entered.created' => 'prepareToUpdateCreditorsJson',
  25.             'workflow.sepa_transaction_creating.entered.created' => 'prepareToUpdateCreditorsJson',
  26.             'workflow.starling_payment_outbound.entered.created' => 'prepareToUpdateCreditorsJson',
  27.             'workflow.swift_outbound.entered.created' => 'prepareToUpdateCreditorsJson',
  28.         ];
  29.     }
  30.     public function prepareToUpdateCreditorsJson(Event $event)
  31.     {
  32.         /** @var Transaction $transaction */
  33.         $transaction $event->getSubject()->getEntity();
  34.         /** @var Client $debitor */
  35.         if (!$debitor $transaction->getDebtorClient()) {
  36.             return;
  37.         }
  38.         if ($debitor->isPerson()) {
  39.             $settings $debitor->getPerson()->getPersonSettings();
  40.             if (!$settings) {
  41.                 return;
  42.             }
  43.             $settings->setCreditorsPrepareAt($transaction->getCreatedAt());
  44.             $this->entityManager->persist($settings);
  45.             $this->entityManager->flush($settings);
  46.             return;
  47.         }
  48.         $settings $debitor->getCompany()->getCompanySettings();
  49.         if (!$settings) {
  50.             return;
  51.         }
  52.         $settings->setCreditorsPrepareAt($transaction->getCreatedAt());
  53.         $this->entityManager->persist($settings);
  54.         $this->entityManager->flush($settings);
  55.     }
  56. }