← Back
Editing: ElevatorBreakdownService.php
<?php namespace App\Service; use App\Entity\Adresse; use App\Repository\AdresseRepository; use Symfony\Component\Security\Core\User\UserInterface; class ElevatorBreakdownService { public function __construct( private readonly AdresseRepository $adresseRepository ) { } /** * @return array<Adresse> */ public function sortByBreakdown(array $addresses, $sort = 'DESC'): array { uasort( $addresses, function ($a, $b) use ($sort) { if ('DESC' === $sort) { return ($a[0]->isBreakdown() >= $b[0]->isBreakdown()) ? -1 : 1; } return ($b[0]->isBreakdown() >= $a[0]->isBreakdown()) ? -1 : 1; } ); return $addresses; } public function getBreakdownDays(Adresse $adresse) { $total = 0; foreach ($adresse->getElevatorBreakdowns() as $breakdown) { $endDate = $breakdown->getEndDate(); if (!$endDate) { $endDate = new \DateTime(); } $interval = $endDate->diff($breakdown->getStartDate()); $minutes = $interval->days * 24 * 60; $minutes += $interval->h * 60; $minutes += $interval->i; $total += $minutes; } return $total / 60 / 24; } public function groupBreakdownsByMonth( UserInterface $user, \DateTimeInterface $startDate, \DateTimeInterface $endDate ): array { $array = []; $formatter = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::TRADITIONAL, \IntlDateFormatter::TRADITIONAL); $formatter->setPattern('MMM yy'); $newEndDate = clone $endDate; $newEndDate->modify('last day of this month'); $period = new \DatePeriod($startDate, new \DateInterval('P1M'), $newEndDate); /** @var \DateTime $i */ foreach ($period as $i) { $date = $formatter->format($i); array_key_exists($date, $array) ?: $array[$date] = 0; } $addresses = $this->adresseRepository->getByManager($user); foreach ($addresses as $address) { foreach ($address[0]->getElevatorBreakdowns() as $breakdown) { $dateBreakdown = $formatter->format($breakdown->getStartDate()); if (array_key_exists($dateBreakdown, $array)) { ++$array[$dateBreakdown]; } } } return $array; } }
Save File
Cancel