src/EventSubscriber/CommunityPostEventSubscriber.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\CommunityPlp;
  5. use App\Entity\CommunityPost;
  6. use App\Entity\Registers;
  7. use App\Entity\TheProperty;
  8. use App\Entity\User;
  9. use App\Mybase\Services\Base\SBase;
  10. use App\Service\Formule;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Event\ViewEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\Security\Core\Security;
  19. use Symfony\Component\Serializer\SerializerInterface;
  20. class CommunityPostEventSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var EntityManagerInterface
  24.      */
  25.     private $manager;
  26.     /**
  27.      * @var SerializerInterface
  28.      */
  29.     private $serializer;
  30.     /**
  31.      * @var ParameterBagInterface
  32.      */
  33.     private $parameterBag;
  34.     /**
  35.      * @var SBase
  36.      */
  37.     private $sbase;
  38.     /**
  39.      * @var User
  40.      */
  41.     private $userToken;
  42.     /**
  43.      * @var Formule
  44.      */
  45.     private $formule;
  46.     public function __construct(
  47.         EntityManagerInterface $manager,
  48.         SerializerInterface $serializer,
  49.         ParameterBagInterface $parameterBag,
  50.         SBase $sbase,
  51.         Security $security,
  52.         Formule $formule
  53.     ) {
  54.         $this->manager $manager;
  55.         $this->serializer $serializer;
  56.         $this->parameterBag $parameterBag;
  57.         $this->sbase $sbase;
  58.         $this->userToken $security->getUser();
  59.         $this->formule $formule;
  60.     }
  61.     /**
  62.      * @return array<string, mixed>
  63.      */
  64.     public static function getSubscribedEvents(): array
  65.     {
  66.         return [
  67.             KernelEvents::VIEW => ['updateData'EventPriorities::PRE_WRITE],
  68.         ];
  69.     }
  70.     /**
  71.      * update the data linked to the community post.
  72.      *
  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 ($content === []) {
  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. //                if (isset($content['coordinates'])) {
  135. //                    if (!$this->formule->ifinsidepolygone([
  136. //                        'lat' => $object->getLatitude(),
  137. //                        'lng' => $object->getLongitude()
  138. //                    ], $content['coordinates'])) {
  139. //                        return $event->setResponse(new JsonResponse(['message' => 'Veuillez saisir une adresse à l\'interieur de votre quartier'], 400));
  140. //                    }
  141. //                }
  142.             } elseif (isset($content['idProperty'])) {
  143.                 $property $this->manager->getRepository(TheProperty::class)->find((int) $content['idProperty']);
  144.                 $object->setLatitude($property->getLatitude())
  145.                     ->setLongitude($property->getLongitude());
  146.             } else {
  147.                 $object->setLatitude($this->userToken->getLatitude())
  148.                     ->setLongitude($this->userToken->getLongitude());
  149.             }
  150.         }
  151.         if ($object instanceof Registers && Request::METHOD_POST) {
  152.             foreach ($object->getRegisterFile() as $registerFile) {
  153.                 if (!$registerFile->getFile() instanceof \Symfony\Component\HttpFoundation\File\File) {
  154.                     return $event->setResponse(new JsonResponse(['message' => 'Vous devriez envoyer au moins un fichier'], \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST));
  155.                 }
  156.             }
  157.         }
  158.     }
  159. }