src/Events/AgainstProposalCounterOfferPostSubscriber.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use App\Entity\AgainstProposal;
  4. use App\Entity\CounterOffer;
  5. use App\Services\Api\AgainstProposalServices;
  6. use App\Services\ConstGeoData\LocationProcessConstant;
  7. use App\Services\Exception\AgainstProposalCounterOfferException;
  8. use Doctrine\ORM\ORMException;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Twig\Error\LoaderError;
  14. use Twig\Error\RuntimeError;
  15. use Twig\Error\SyntaxError;
  16. class AgainstProposalCounterOfferPostSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var AgainstProposalServices
  20.      */
  21.     private $services;
  22.     /**
  23.      * AgainstProposalCounterOfferPostSubscriber constructor.
  24.      * @param AgainstProposalServices $services
  25.      */
  26.     public function __construct(AgainstProposalServices $services)
  27.     {
  28.         $this->services $services;
  29.     }
  30.     /**
  31.      * @return array|string[]
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             RequestEvent::class => 'onKernelRequest',
  37.             ViewEvent::class => ['onKernelView'10]
  38.         ];
  39.     }
  40.     /**
  41.      * @param RequestEvent $event
  42.      * @throws AgainstProposalCounterOfferException
  43.      */
  44.     public function onKernelRequest(RequestEvent $event): void
  45.     {
  46.         $request $event->getRequest();
  47.         $resource $request->attributes->get("_api_resource_class");
  48.         if (
  49.             ($resource === CounterOffer::class || $resource === AgainstProposal::class)
  50.             && $request->getMethod() === Request::METHOD_POST
  51.         ) {
  52.             $data json_decode($request->getContent(), true);
  53.             if ($this->services->isRent($data$resource) === true) {
  54.                 throw new AgainstProposalCounterOfferException("the property not yet available");
  55.             }
  56.             if (!array_key_exists('status'$data)) {
  57.                 throw new AgainstProposalCounterOfferException("request data must contains status");
  58.             }
  59.             if (!in_array(strtolower($data['status']), LocationProcessConstant::allStatusProposalOffer(), true)) {
  60.                 throw new AgainstProposalCounterOfferException("status invalid");
  61.             }
  62.             if ($this->services->haveProposalPending($data$resource) === true) {
  63.                 throw new AgainstProposalCounterOfferException("you already have a pending proposal");
  64.             }
  65.             if ($resource === CounterOffer::class && $this->services->isUserLessor($data) === false) {
  66.                 throw new AgainstProposalCounterOfferException("user not Lessor is unauthorized");
  67.             }
  68.             if ($resource === AgainstProposal::class && $this->services->isUserLessor($data) === true) {
  69.                 throw new AgainstProposalCounterOfferException("user Lessor is unauthorized to make a proposal");
  70.             }
  71.         }
  72.     }
  73.     /**
  74.      * @param ViewEvent $event
  75.      * @throws LoaderError
  76.      * @throws RuntimeError
  77.      * @throws SyntaxError
  78.      * @throws ORMException
  79.      */
  80.     public function onKernelView(ViewEvent $event): void
  81.     {
  82.         $request $event->getRequest();
  83.         $resource $request->attributes->get('_api_resource_class');
  84.         if (
  85.             ($resource === CounterOffer::class || $resource === AgainstProposal::class)
  86.             && $request->getMethod() === Request::METHOD_POST
  87.         ) {
  88.             if ($data $event->getControllerResult()) {
  89.                 $requestData json_decode($request->getContent(), true);
  90.                 $this->services->postProcess($resource$data$requestData);
  91.             }
  92.         }
  93.     }
  94. }