<?php
namespace App\Controller;
use App\Entity\Profile\Profile;
use App\Event\Profile\ProfilesShownEvent;
use App\Event\Profile\ProfileViewEvent;
use App\Repository\ProfileRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProfileCtrController extends AbstractController
{
#[Route(path: '/increase-shows/', name: 'profile_ctr.increase_shows')]
public function increaseShows(Request $request, ProfileRepository $profileRepository, EventDispatcherInterface $eventDispatcher): Response
{
$content = json_decode($request->getContent(), true);
$profileIds = $content['profiles'] ?? [];
$source = $content['source'] ?? '';
$profiles = $profileRepository->findByIds($profileIds);
$profileIds = array_map(function(Profile $profile): int { return $profile->getId(); }, $profiles);
$eventDispatcher->dispatch(new ProfilesShownEvent($profileIds, $source), ProfilesShownEvent::NAME);
return $this->json([]);
}
#[Route(path: '/increase-views/', name: 'profile_ctr.increase_views')]
public function increaseViews(Request $request, ProfileRepository $profileRepository, EventDispatcherInterface $eventDispatcher): Response
{
$content = json_decode($request->getContent(), true);
$profileId = $content['profile'] ?? null;
$profile = $profileRepository->find($profileId);
if(null === $profile) {
return $this->json(['error' => 'Not found']);
}
$eventDispatcher->dispatch(new ProfileViewEvent($profileId), ProfileViewEvent::NAME);
return $this->json([]);
}
}