← Back
Editing: AddressService.php
<?php namespace App\Service; use App\Entity\Adresse; use App\Entity\Agency; use App\Repository\AdresseRepository; use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\NoResultException; use Symfony\Component\Security\Core\Security; class AddressService { public function __construct( private readonly AdresseRepository $addressRepository, private readonly Security $security ) { } /** * @throws NoResultException * @throws NonUniqueResultException */ public function getControlEvolutionFromPeriod(Adresse $address, int $cnt): float|int { $user = $this->security->getUser(); $qb = $this->addressRepository->getQueryByAgencies( agenciesId: [$user->getAgency()->getId()], address: $address, previous: true ); $qb->select('COUNT(c.id)'); $result = $qb->getQuery()->getSingleScalarResult(); if ($result > 0) { $evolution = (($cnt - $result) / $result) * 100; } else { $evolution = 0; } return $evolution; } /** * @throws NoResultException * @throws NonUniqueResultException */ public function getControlEvolutionFromPeriodForOrganization(Adresse $address, int $cnt): float|int { $user = $this->security->getUser(); $agenciesId = array_map(function (Agency $agency) { return $agency->getId(); }, $user->getOrganization()->getAgencies()->toArray() ); $qb = $this->addressRepository->getQueryByAgencies($agenciesId, $address); $qb->select('COUNT(c.id)'); $result = $qb->getQuery()->getSingleScalarResult(); if ($result > 0) { $evolution = (($cnt - $result) / $result) * 100; } else { $evolution = 0; } return $evolution; } public function getAgencyByAddressId(?int $addressId): ?Agency { return is_null($addressId) ? null : $this->addressRepository->find($addressId)?->getAgency(); } }
Save File
Cancel