<?php
namespace App\Events;
use App\Entity\AgainstProposal;
use App\Entity\CounterOffer;
use App\Services\Api\AgainstProposalServices;
use App\Services\ConstGeoData\LocationProcessConstant;
use App\Services\Exception\AgainstProposalCounterOfferException;
use Doctrine\ORM\ORMException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class AgainstProposalCounterOfferPostSubscriber implements EventSubscriberInterface
{
/**
* @var AgainstProposalServices
*/
private $services;
/**
* AgainstProposalCounterOfferPostSubscriber constructor.
* @param AgainstProposalServices $services
*/
public function __construct(AgainstProposalServices $services)
{
$this->services = $services;
}
/**
* @return array|string[]
*/
public static function getSubscribedEvents(): array
{
return [
RequestEvent::class => 'onKernelRequest',
ViewEvent::class => ['onKernelView', 10]
];
}
/**
* @param RequestEvent $event
* @throws AgainstProposalCounterOfferException
*/
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
$resource = $request->attributes->get("_api_resource_class");
if (
($resource === CounterOffer::class || $resource === AgainstProposal::class)
&& $request->getMethod() === Request::METHOD_POST
) {
$data = json_decode($request->getContent(), true);
if ($this->services->isRent($data, $resource) === true) {
throw new AgainstProposalCounterOfferException("the property not yet available");
}
if (!array_key_exists('status', $data)) {
throw new AgainstProposalCounterOfferException("request data must contains status");
}
if (!in_array(strtolower($data['status']), LocationProcessConstant::allStatusProposalOffer(), true)) {
throw new AgainstProposalCounterOfferException("status invalid");
}
if ($this->services->haveProposalPending($data, $resource) === true) {
throw new AgainstProposalCounterOfferException("you already have a pending proposal");
}
if ($resource === CounterOffer::class && $this->services->isUserLessor($data) === false) {
throw new AgainstProposalCounterOfferException("user not Lessor is unauthorized");
}
if ($resource === AgainstProposal::class && $this->services->isUserLessor($data) === true) {
throw new AgainstProposalCounterOfferException("user Lessor is unauthorized to make a proposal");
}
}
}
/**
* @param ViewEvent $event
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws ORMException
*/
public function onKernelView(ViewEvent $event): void
{
$request = $event->getRequest();
$resource = $request->attributes->get('_api_resource_class');
if (
($resource === CounterOffer::class || $resource === AgainstProposal::class)
&& $request->getMethod() === Request::METHOD_POST
) {
if ($data = $event->getControllerResult()) {
$requestData = json_decode($request->getContent(), true);
$this->services->postProcess($resource, $data, $requestData);
}
}
}
}