src/Events/CommunityPostEventSubscriber.php line 84

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use App\Entity\User;
  4. use App\Entity\TheProperty;
  5. use App\Entity\CommunityPost;
  6. use App\Mybase\Services\Base\SBase;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use ApiPlatform\Core\EventListener\EventPriorities;
  12. use App\Entity\Registers;
  13. use Symfony\Component\Serializer\SerializerInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\Security\Core\Security;
  18. class CommunityPostEventSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var EntityManagerInterface
  22.      */
  23.     private $manager;
  24.     /**
  25.      * @var SerializerInterface
  26.      */
  27.     private $serializer;
  28.     /**
  29.      * @var ParameterBagInterface
  30.      */
  31.     private $parameterBag;
  32.     /**
  33.      * @var SBase
  34.      */
  35.     private $sbase;
  36.     /**
  37.      * @var User
  38.      */
  39.     private $userToken;
  40.     /**
  41.      * @param EntityManagerInterface $manager
  42.      * @param SerializerInterface $serializer
  43.      * @param ParameterBagInterface $parameterBag
  44.      * @param SBase $sbase
  45.      * @param Security $security
  46.      */
  47.     public function __construct(
  48.         EntityManagerInterface $manager,
  49.         SerializerInterface $serializer,
  50.         ParameterBagInterface $parameterBag,
  51.         SBase $sbase,
  52.         Security $security
  53.     ) {
  54.         $this->manager $manager;
  55.         $this->serializer $serializer;
  56.         $this->parameterBag $parameterBag;
  57.         $this->sbase $sbase;
  58.         $this->userToken $security->getUser();
  59.     }
  60.     /**
  61.      * @return array<string, mixed>
  62.      */
  63.     public static function getSubscribedEvents(): array
  64.     {
  65.         return [
  66.             KernelEvents::VIEW => ['updateData'EventPriorities::PRE_WRITE,]
  67.         ];
  68.     }
  69.     /**
  70.      * update the data linked to the community post
  71.      *
  72.      * @param ViewEvent $event
  73.      * @return void
  74.      */
  75.     public function updateData(ViewEvent $event)
  76.     {
  77.         $object $event->getControllerResult();
  78.         $request $event->getRequest();
  79.         /**
  80.          * update user profile in community
  81.          */
  82.         if ($object instanceof User && $request->getMethod() == Request::METHOD_PUT) {
  83.             $this->sbase->save($object);
  84.             $data $this->sbase->serialize($object"json", [
  85.                 "groups" => "community:profile:read"
  86.             ]);
  87.             $posts $this->sbase->serialize($object->getCommunityPosts(), "json", [
  88.                 "groups" => "community:post:read"
  89.             ]);
  90.             $posts json_decode($posts);
  91.             $data  json_decode($data);
  92.             $data->posts $posts;
  93.             unset($data->communityPosts);
  94.             $data json_encode($data);
  95.             $event->setResponse($this->sbase->jsonResponseOk($data"user"));
  96.             return;
  97.         }
  98.         /**
  99.          * update object relation in community post
  100.          */
  101.         if ($object instanceof CommunityPost && Request::METHOD_POST === $request->getMethod()) {
  102.             $content     $request->request->all();
  103.             $context     = ["groups" => 'community:post:write'];
  104.             if (count($content) === 0) {
  105.                 $content = (array) json_decode($request->getContent());
  106.             }
  107.             $methods   = [];
  108.             if (isset($content["type"])) {
  109.                 if ($content["type"] === "plp") {
  110.                     $plp $this->serializer->deserialize(json_encode($content), CommunityPlp::class, "json"$context);
  111.                     $plp->setPdf($this->parameterBag->get("community_plp") . uniqid() . '.pdf');
  112.                     $object->setPlp($plp);
  113.                     $methods get_class_methods($plp);
  114.                     unset($methods[array_search("setPlp"$methods)]);
  115.                 } else {
  116.                     $types    $object->getTypes();
  117.                     if (isset($types[$content['type']])) {
  118.                         $class    $types[$content['type']]['class'];
  119.                         $method   $types[$content['type']]['method'];
  120.                         $relation $this->serializer->deserialize(json_encode($content), $class"json"$context);
  121.                         $object->$method($relation);
  122.                         $methods get_class_methods($relation);
  123.                     }
  124.                 }
  125.             }
  126.             foreach ($methods as $method) {
  127.                 if ($method[0] == "s" && method_exists($object$method)) {
  128.                     $object->$method(null);
  129.                 }
  130.             }
  131.             if (isset($content["latitude"]) && isset($content["longitude"])) {
  132.                 $object->setLatitude($content["latitude"])
  133.                        ->setLongitude($content["longitude"]);
  134.             } elseif (isset($content["idProperty"])) {
  135.                 $property  $this->manager->getRepository(TheProperty::class)->find((int) $content["idProperty"]);
  136.                 $object->setLatitude($property->getLatitude())
  137.                     ->setLongitude($property->getLongitude());
  138.             } else {
  139.                 $object->setLatitude($this->userToken->getLatitude())
  140.                        ->setLongitude($this->userToken->getLongitude());
  141.             }
  142.         }
  143.         if($object instanceof Registers && Request::METHOD_POST) {
  144.             foreach($object->getRegisterFile() as $registerFile) {
  145.                 if($registerFile->getFile() === null) {
  146.                     return $event->setResponse(new JsonResponse(['message' => 'Vous devriez envoyer au moins un fichier'], 400));
  147.                 }
  148.             }
  149.         }
  150.     }
  151. }