<?php
namespace App\Service;
use App\Entity\Club;
use App\Entity\Company;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
class ClubHandler
{
public function __construct(
private readonly EntityManagerInterface $em,
private RequestStack $requestStack,
private Security $security,
) {
}
public function getCompanies(Club $club, array $params) : array {
return $this->em->getRepository(Company::class)->getCompaniesFromClub($club, $params);
}
public function getNearestClub(array $params) : array {
return $this->em->getRepository(Club::class)->getClubLocalize($params);
}
public function getCurrentBoClub() : ?Club {
$session = $this->requestStack->getSession();
$user = $this->security->getUser();
$currentBoClubId = $session->get('currentBoClubId');
if ($currentBoClubId) {
return $this->em->getRepository(Club::class)->find($currentBoClubId);
}
$clubs = $this->em->getRepository(Club::class)->findManagedClubsByUser($user);
$currentBoClub = sizeof($clubs) > 0 ? $clubs[0] : null;
if ($currentBoClub) {
$session->set('currentBoClubId', $currentBoClub->getId());
}
return $currentBoClub;
}
public function setCurrentBoClub(int $boClubId) : ?Club {
$session = $this->requestStack->getSession();
$currentBoClub = $this->em->getRepository(Club::class)->find($boClubId);
if ($currentBoClub) {
$session->set('currentBoClubId', $currentBoClub->getId());
}
return $currentBoClub;
}
}