<?php
namespace App\Modules\Contract\Event\Subscriber;
use App\Modules\Contract\Entity\Contract;
use App\Modules\Contract\Event\WarrantyFormUpdated;
use App\Modules\Contract\Service\ContractService;
use App\Modules\Contract\StateMachine\WarrantyFormState;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ChangeWarrantyContractStatus 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 [
WarrantyFormUpdated::class => 'onWarrantyFormUpdated',
];
}
public function onWarrantyFormUpdated(WarrantyFormUpdated $event)
{
$form = $event->getForm();
$contract = $form->getContract();
if (is_null($contract)) {
return;
}
$formProgress = $form->getState();
if (in_array($formProgress, WarrantyFormState::$inProgressStatuses)) {
$this->contractService->updateStatus($contract, Contract::STATUS_IN_PROGRESS);
} elseif (in_array($formProgress, WarrantyFormState::$claimedStatuses)) {
$this->contractService->updateStatus($contract, Contract::STATUS_CLAIMED);
} elseif (in_array($formProgress, WarrantyFormState::$disputedStatuses)) {
$this->contractService->updateStatus($contract, Contract::STATUS_DISPUTED);
} elseif (in_array($formProgress, WarrantyFormState::$cancelledStatus)) {
$this->contractService->updateStatus($contract, Contract::STATUS_CANCELLED);
} elseif (in_array($formProgress, WarrantyFormState::$acceptedButNeverCompletedStatus)) {
$this->contractService->updateStatus($contract, Contract::STATUS_ACCEPTED_BUT_NEVER_COMPLETED);
} elseif (in_array($formProgress, WarrantyFormState::$rejectedStatuses)) {
$this->contractService->updateStatus($contract, Contract::STATUS_REJECTED);
} elseif (in_array($formProgress, WarrantyFormState::$completedStatuses)) {
$this->contractService->updateStatus($contract, Contract::STATUS_COMPLETED);
}
}
}