1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- namespace AppBundle\Service;
- use AppBundle\Entity\Film;
- use Doctrine\ORM\EntityManagerInterface;
- /**
- * Calculer moyenne pour un film
- */
- class NoteMoyenne {
- private $em;
- public function __construct(EntityManagerInterface $em)
- {
- $this->em = $em;
- }
- public function calculerMoyenne (Film $film)
- {
- $commentaires = $this->em
- ->getRepository('AppBundle:Commentaire')
- ->findBy(array('film' => $film));
- $notes[] = array();
- $total = 0;
- $nb = 0;
- foreach ($commentaires as $commentaire) {
- if ($commentaire->getNote() > 0 && $commentaire->getNote() != null)
- $nb++;
- $total += $commentaire->getNote();
- }
- $note = 0;
- if ($nb > 0) {
- $note = $total / $nb;
- }
- if ($note == 0)
- $note = null;
- $film->setNote($note);
- $film->setNbComs(count($commentaires));
- $this->em->flush();
- }
- }
|