|
@@ -0,0 +1,44 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace AppBundle\Service;
|
|
|
+
|
|
|
+use AppBundle\Entity\Commentaire;;
|
|
|
+use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
|
|
|
+
|
|
|
+
|
|
|
+class NoteListener
|
|
|
+{
|
|
|
+ private $noteMoyenne;
|
|
|
+
|
|
|
+ public function __construct(NoteMoyenne $noteMoyenne)
|
|
|
+ {
|
|
|
+ $this->noteMoyenne = $noteMoyenne;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function postPersist(LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $this->calculer($args);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public function postUpdate (LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $this->calculer($args);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function postRemove (LifecycleEventArgs $args)
|
|
|
+ {
|
|
|
+ $this->calculer($args);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function calculer (LifecycleEventArgs $args) {
|
|
|
+ $entity = $args->getObject();
|
|
|
+
|
|
|
+ if (!$entity instanceof Commentaire) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->noteMoyenne->calculerMoyenne($entity->getFilm());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|