src/Service/VehicleStatisticService.php line 44
<?phpnamespace App\Service;use App\Entity\Vehicle;use App\Repository\VehicleStatisticRepository;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\RequestStack;class VehicleStatisticService{private const SESSION_KEY_PREFIX = 'vehicle_stat_';public function __construct(private VehicleStatisticRepository $statisticRepository,private EntityManagerInterface $entityManager,private RequestStack $requestStack) {}public function trackAction(Vehicle $vehicle, string $action): bool{$session = $this->requestStack->getSession();$sessionKey = $this->getSessionKey($vehicle->getId(), $action);$today = date('Y-m-d');if ($session->get($sessionKey) === $today) {return false;}$currentMonth = (int) date('n');$currentYear = (int) date('Y');$statistic = $this->statisticRepository->addOrIncrementStatistic($vehicle,$action,$currentMonth,$currentYear);$this->entityManager->flush();$session->set($sessionKey, $today);return true;}public function canTrackAction(Vehicle $vehicle, string $action): bool{$session = $this->requestStack->getSession();$sessionKey = $this->getSessionKey($vehicle->getId(), $action);$today = date('Y-m-d');return $session->get($sessionKey) !== $today;}private function getSessionKey(int $vehicleId, string $action): string{return self::SESSION_KEY_PREFIX . $vehicleId . '_' . $action;}}