<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\CommunityPlp;
use App\Entity\CommunityPost;
use App\Entity\Registers;
use App\Entity\TheProperty;
use App\Entity\User;
use App\Mybase\Services\Base\SBase;
use App\Service\Formule;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
class CommunityPostEventSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $manager;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var ParameterBagInterface
*/
private $parameterBag;
/**
* @var SBase
*/
private $sbase;
/**
* @var User
*/
private $userToken;
/**
* @var Formule
*/
private $formule;
public function __construct(
EntityManagerInterface $manager,
SerializerInterface $serializer,
ParameterBagInterface $parameterBag,
SBase $sbase,
Security $security,
Formule $formule
) {
$this->manager = $manager;
$this->serializer = $serializer;
$this->parameterBag = $parameterBag;
$this->sbase = $sbase;
$this->userToken = $security->getUser();
$this->formule = $formule;
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => ['updateData', EventPriorities::PRE_WRITE],
];
}
/**
* update the data linked to the community post.
*
* @return void
*/
public function updateData(ViewEvent $event)
{
$object = $event->getControllerResult();
$request = $event->getRequest();
/*
* update user profile in community
*/
if ($object instanceof User && $request->getMethod() == Request::METHOD_PUT) {
$this->sbase->save($object);
$data = $this->sbase->serialize($object, 'json', [
'groups' => 'community:profile:read',
]);
$posts = $this->sbase->serialize($object->getCommunityPosts(), 'json', [
'groups' => 'community:post:read',
]);
$posts = json_decode($posts);
$data = json_decode($data);
$data->posts = $posts;
unset($data->communityPosts);
$data = json_encode($data);
$event->setResponse($this->sbase->jsonResponseOk($data, 'user'));
return;
}
/*
* update object relation in community post
*/
if ($object instanceof CommunityPost && Request::METHOD_POST === $request->getMethod()) {
$content = $request->request->all();
$context = ['groups' => 'community:post:write'];
if ($content === []) {
$content = (array) json_decode($request->getContent());
}
$methods = [];
if (isset($content['type'])) {
if ($content['type'] === 'plp') {
$plp = $this->serializer->deserialize(json_encode($content), CommunityPlp::class, 'json', $context);
$plp->setPdf($this->parameterBag->get('community_plp').uniqid().'.pdf');
$object->setPlp($plp);
$methods = get_class_methods($plp);
unset($methods[array_search('setPlp', $methods)]);
} else {
$types = $object->getTypes();
if (isset($types[$content['type']])) {
$class = $types[$content['type']]['class'];
$method = $types[$content['type']]['method'];
$relation = $this->serializer->deserialize(json_encode($content), $class, 'json', $context);
$object->$method($relation);
$methods = get_class_methods($relation);
}
}
}
foreach ($methods as $method) {
if ($method[0] == 's' && method_exists($object, $method)) {
$object->$method(null);
}
}
if (isset($content['latitude']) && isset($content['longitude'])) {
$object->setLatitude($content['latitude'])
->setLongitude($content['longitude']);
// if (isset($content['coordinates'])) {
// if (!$this->formule->ifinsidepolygone([
// 'lat' => $object->getLatitude(),
// 'lng' => $object->getLongitude()
// ], $content['coordinates'])) {
// return $event->setResponse(new JsonResponse(['message' => 'Veuillez saisir une adresse à l\'interieur de votre quartier'], 400));
// }
// }
} elseif (isset($content['idProperty'])) {
$property = $this->manager->getRepository(TheProperty::class)->find((int) $content['idProperty']);
$object->setLatitude($property->getLatitude())
->setLongitude($property->getLongitude());
} else {
$object->setLatitude($this->userToken->getLatitude())
->setLongitude($this->userToken->getLongitude());
}
}
if ($object instanceof Registers && Request::METHOD_POST) {
foreach ($object->getRegisterFile() as $registerFile) {
if (!$registerFile->getFile() instanceof \Symfony\Component\HttpFoundation\File\File) {
return $event->setResponse(new JsonResponse(['message' => 'Vous devriez envoyer au moins un fichier'], \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST));
}
}
}
}
}