src/Modules/Contract/Event/Subscriber/ChangeContractStatus.php line 72

Open in your IDE?
  1. <?php
  2. namespace App\Modules\Contract\Event\Subscriber;
  3. use App\Modules\Contract\Entity\Contract;
  4. use App\Modules\Contract\Entity\Form\Warranty;
  5. use App\Modules\Contract\Event\ContractCreated;
  6. use App\Modules\Contract\StateMachine\FormState;
  7. use App\Modules\Contract\Event\FormAccepted;
  8. use App\Modules\Contract\Event\FormDeclined;
  9. use App\Modules\Contract\Event\FormFinalized;
  10. use App\Modules\Contract\Event\FormUpdated;
  11. use App\Modules\Contract\Service\ContractService;
  12. use App\Modules\Contract\ValueObject\FormValueObject;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ChangeContractStatus implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var ContractService
  18.      */
  19.     private $contractService;
  20.     public function __construct(ContractService $contractService)
  21.     {
  22.         $this->contractService $contractService;
  23.     }
  24.     /**
  25.      * Returns an array of event names this subscriber wants to listen to.
  26.      *
  27.      * The array keys are event names and the value can be:
  28.      *
  29.      *  * The method name to call (priority defaults to 0)
  30.      *  * An array composed of the method name to call and the priority
  31.      *  * An array of arrays composed of the method names to call and respective
  32.      *    priorities, or 0 if unset
  33.      *
  34.      * For instance:
  35.      *
  36.      *  * ['eventName' => 'methodName']
  37.      *  * ['eventName' => ['methodName', $priority]]
  38.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  39.      *
  40.      * @return array The event names to listen to
  41.      */
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             ContractCreated::class => 'onContractCreated',
  46.             FormUpdated::class => 'onFormUpdated',
  47.             FormAccepted::class => 'onFormAccepted',
  48.             FormDeclined::class => 'onFormDeclined',
  49.             FormFinalized::class => 'onFormFinalized'
  50.         ];
  51.     }
  52.     public function onContractCreated(ContractCreated $event)
  53.     {
  54.         $contract $event->getContract();
  55.         $form $contract->getForm();
  56.         $status Contract::STATUS_INITIALIZE;
  57.         if ($form->getType() == Warranty::TYPE) {
  58.             $status Contract::STATUS_REPORTED;
  59.         }
  60.         $this->contractService->updateStatus($contract$status);
  61.     }
  62.     public function onFormUpdated(FormUpdated $event)
  63.     {
  64.         $contract $event->getForm()->getContract();
  65.         if (!is_null($contract)) {
  66.             $this->contractService->initializeContract($contract$event->getValue());
  67.         }
  68.     }
  69.     public function onFormAccepted(FormAccepted $event)
  70.     {
  71.         $form $event->getForm();
  72.         $contract $form->getContract();
  73.         if (!is_null($contract) && $form->acceptedByExecutor()) {
  74.             $this->contractService->startContract($contract);
  75.         }
  76.     }
  77.     public function onFormDeclined(FormDeclined $event)
  78.     {
  79.         $form $event->getForm();
  80.         $contract $form->getContract();
  81.         if (is_null($contract)) {
  82.             return;
  83.         }
  84.         $formProgress $form->getState();
  85.         if ($formProgress == FormState::STATUS_INITIATOR_DECLINE_VALUE) {
  86.             $this->contractService->declineContract($contract$contract->getInitiator());
  87.         } elseif ($formProgress == FormState::STATUS_EXECUTOR_DECLINE_VALUE) {
  88.             $this->contractService->declineContract($contract$contract->getExecutor());
  89.         }
  90.     }
  91.     public function onFormFinalized(FormFinalized $event)
  92.     {
  93.         $contract $event->getForm()->getContract();
  94.         if (!is_null($contract)) {
  95.             $this->contractService->finalizeContract($contract);
  96.         }
  97.     }
  98. }