<?php
namespace App\Events;
use App\Entity\User;
use App\Entity\TheProperty;
use App\Entity\CommunityPost;
use App\Mybase\Services\Base\SBase;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Registers;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Security;
class CommunityPostEventSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $manager;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var ParameterBagInterface
*/
private $parameterBag;
/**
* @var SBase
*/
private $sbase;
/**
* @var User
*/
private $userToken;
/**
* @param EntityManagerInterface $manager
* @param SerializerInterface $serializer
* @param ParameterBagInterface $parameterBag
* @param SBase $sbase
* @param Security $security
*/
public function __construct(
EntityManagerInterface $manager,
SerializerInterface $serializer,
ParameterBagInterface $parameterBag,
SBase $sbase,
Security $security
) {
$this->manager = $manager;
$this->serializer = $serializer;
$this->parameterBag = $parameterBag;
$this->sbase = $sbase;
$this->userToken = $security->getUser();
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => ['updateData', EventPriorities::PRE_WRITE,]
];
}
/**
* update the data linked to the community post
*
* @param ViewEvent $event
* @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 (count($content) === 0) {
$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"]);
} 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() === null) {
return $event->setResponse(new JsonResponse(['message' => 'Vous devriez envoyer au moins un fichier'], 400));
}
}
}
}
}