src/Controller/ProfileCtrController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Profile\Profile;
  4. use App\Event\Profile\ProfilesShownEvent;
  5. use App\Event\Profile\ProfileViewEvent;
  6. use App\Repository\ProfileRepository;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class ProfileCtrController extends AbstractController
  13. {
  14.     #[Route(path'/increase-shows/'name'profile_ctr.increase_shows')]
  15.     public function increaseShows(Request $requestProfileRepository $profileRepositoryEventDispatcherInterface $eventDispatcher): Response
  16.     {
  17.         $content json_decode($request->getContent(), true);
  18.         $profileIds $content['profiles'] ?? [];
  19.         $source $content['source'] ?? '';
  20.         $profiles $profileRepository->findByIds($profileIds);
  21.         $profileIds array_map(function(Profile $profile): int { return $profile->getId(); }, $profiles);
  22.         $eventDispatcher->dispatch(new ProfilesShownEvent($profileIds$source), ProfilesShownEvent::NAME);
  23.         return $this->json([]);
  24.     }
  25.     #[Route(path'/increase-views/'name'profile_ctr.increase_views')]
  26.     public function increaseViews(Request $requestProfileRepository $profileRepositoryEventDispatcherInterface $eventDispatcher): Response
  27.     {
  28.         $content json_decode($request->getContent(), true);
  29.         $profileId $content['profile'] ?? null;
  30.         $profile $profileRepository->find($profileId);
  31.         if(null === $profile) {
  32.             return $this->json(['error' => 'Not found']);
  33.         }
  34.         $eventDispatcher->dispatch(new ProfileViewEvent($profileId), ProfileViewEvent::NAME);
  35.         return $this->json([]);
  36.     }
  37. }