<?php
namespace App\Services\Transaction\Workflow\EventSubscriber\Internal;
use App\Entity\Client;
use App\Entity\Transaction;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
class UpdateCreditorsJsonSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManager
*/
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
/** @var EntityManager $em */
$em = $entityManager;
$this->entityManager = $em;
}
public static function getSubscribedEvents()
{
return [
'workflow.internal_transaction_creating.entered.created' => 'prepareToUpdateCreditorsJson',
'workflow.sepa_transaction_creating.entered.created' => 'prepareToUpdateCreditorsJson',
'workflow.starling_payment_outbound.entered.created' => 'prepareToUpdateCreditorsJson',
'workflow.swift_outbound.entered.created' => 'prepareToUpdateCreditorsJson',
];
}
public function prepareToUpdateCreditorsJson(Event $event)
{
/** @var Transaction $transaction */
$transaction = $event->getSubject()->getEntity();
/** @var Client $debitor */
if (!$debitor = $transaction->getDebtorClient()) {
return;
}
if ($debitor->isPerson()) {
$settings = $debitor->getPerson()->getPersonSettings();
if (!$settings) {
return;
}
$settings->setCreditorsPrepareAt($transaction->getCreatedAt());
$this->entityManager->persist($settings);
$this->entityManager->flush($settings);
return;
}
$settings = $debitor->getCompany()->getCompanySettings();
if (!$settings) {
return;
}
$settings->setCreditorsPrepareAt($transaction->getCreatedAt());
$this->entityManager->persist($settings);
$this->entityManager->flush($settings);
}
}