<?php
namespace App\Modules\Contract\Event\Subscriber;
use App\Modules\Contract\Entity\Contract;
use App\Modules\Contract\Entity\Form\Warranty;
use App\Modules\Contract\Event\ContractCreated;
use App\Modules\Contract\StateMachine\FormState;
use App\Modules\Contract\Event\FormAccepted;
use App\Modules\Contract\Event\FormDeclined;
use App\Modules\Contract\Event\FormFinalized;
use App\Modules\Contract\Event\FormUpdated;
use App\Modules\Contract\Service\ContractService;
use App\Modules\Contract\ValueObject\FormValueObject;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ChangeContractStatus implements EventSubscriberInterface
{
/**
* @var ContractService
*/
private $contractService;
public function __construct(ContractService $contractService)
{
$this->contractService = $contractService;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
ContractCreated::class => 'onContractCreated',
FormUpdated::class => 'onFormUpdated',
FormAccepted::class => 'onFormAccepted',
FormDeclined::class => 'onFormDeclined',
FormFinalized::class => 'onFormFinalized'
];
}
public function onContractCreated(ContractCreated $event)
{
$contract = $event->getContract();
$form = $contract->getForm();
$status = Contract::STATUS_INITIALIZE;
if ($form->getType() == Warranty::TYPE) {
$status = Contract::STATUS_REPORTED;
}
$this->contractService->updateStatus($contract, $status);
}
public function onFormUpdated(FormUpdated $event)
{
$contract = $event->getForm()->getContract();
if (!is_null($contract)) {
$this->contractService->initializeContract($contract, $event->getValue());
}
}
public function onFormAccepted(FormAccepted $event)
{
$form = $event->getForm();
$contract = $form->getContract();
if (!is_null($contract) && $form->acceptedByExecutor()) {
$this->contractService->startContract($contract);
}
}
public function onFormDeclined(FormDeclined $event)
{
$form = $event->getForm();
$contract = $form->getContract();
if (is_null($contract)) {
return;
}
$formProgress = $form->getState();
if ($formProgress == FormState::STATUS_INITIATOR_DECLINE_VALUE) {
$this->contractService->declineContract($contract, $contract->getInitiator());
} elseif ($formProgress == FormState::STATUS_EXECUTOR_DECLINE_VALUE) {
$this->contractService->declineContract($contract, $contract->getExecutor());
}
}
public function onFormFinalized(FormFinalized $event)
{
$contract = $event->getForm()->getContract();
if (!is_null($contract)) {
$this->contractService->finalizeContract($contract);
}
}
}