← Back
Editing: ControlPersister.php
<?php namespace App\Service\Control; use App\Entity\Controle; use App\Event\ControlSentEvent; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; class ControlPersister { public function __construct( private readonly EntityManagerInterface $entityManager, private readonly EventDispatcherInterface $dispatcher ) { } public function __invoke(Controle $control, ?array $receivedControl = null): Controle { $this->fillAverageNote($control); $this->entityManager->persist($control); if (null !== $receivedControl) { $event = new ControlSentEvent($control, $receivedControl); $this->dispatcher->dispatch($event); } if ($control->getDateRealisation()) { $this->entityManager->flush(); } return $control; } private function fillAverageNote(Controle $control): void { $totalNote = 0; $totalCount = 0; foreach ($control->getNotes() as $note) { $totalNote += $note->getNote(); ++$totalCount; } if (0 === $totalCount) { return; } $control->setAverageNote($totalNote / $totalCount); } }
Save File
Cancel