src/EventSubscriber/LogJwpEventsSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\FormationRegistration;
  4. use App\Entity\Log;
  5. use App\Events\Events;
  6. use App\Service\Algo\JwpUtils;
  7. use App\Service\formation\FormationService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\EventDispatcher\GenericEvent;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. class LogJwpEventsSubscriber implements EventSubscriberInterface
  13. {
  14.     private $em;
  15.     /**
  16.      * @var TokenStorageInterface
  17.      */
  18.     private $token;
  19.     /**
  20.      * @var FormationService
  21.      */
  22.     private $formationService;
  23.     private JwpUtils $jwpUtils;
  24.     /**
  25.      * LogJwpEventsSubscriber constructor.
  26.      * @param $em
  27.      */
  28.     public function __construct(EntityManagerInterface $emTokenStorageInterface $tokenFormationService  $formationServiceJwpUtils $jwpUtils)
  29.     {
  30.         $this->em $em;
  31.         $this->token $token;
  32.         $this->formationService $formationService;
  33.         $this->jwpUtils $jwpUtils;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             Events::VIDEO_COMPLETE => 'onVideoComplete',
  39.             Events::VIDEO_FORMATION_PROGRESS => 'onVideoFormationProgress',
  40.         ];
  41.     }
  42.     public function onVideoComplete(GenericEvent $event): void
  43.     {
  44.         $data $event->getSubject();
  45.         $user =  $this->token->getToken()->getUser();
  46.         $sources $data['source'];
  47.         $metadata $sources['feedData'];
  48.         if ($metadata['CAT'] === 'ERGO'){
  49.             $sources['visibility'] = $this->jwpUtils->setVisibilitySource($user,$metadata);
  50.         }
  51.         $log =  new Log();
  52.         $log->setUser$user );
  53.         $log->setEvent($data['event']);
  54.         $log->setEntityId($data['mediaid']);
  55.         $log->setSources($sources);
  56.         $this->em->persist($log);
  57.         $this->em->flush();
  58.     }
  59.     public function onVideoFormationProgress(GenericEvent $event): void
  60.     {
  61.         $data $event->getSubject();
  62.         $user $this->token->getToken()->getUser();
  63.         /* @var $log Log*/
  64.         $log $this->em->getRepository(Log::class)->findOneBy([
  65.             'event'=>Events::VIDEO_FORMATION_PROGRESS,
  66.             'entity'=>'formation',
  67.             'entityId'=>$data['entityId'],
  68.             'user'=>$user
  69.         ]);
  70.         //je trouve un log en progress
  71.         if($log){
  72.             //update sources
  73.             $videos $log->getSources()['items'];
  74.             array_push($videos,$data['mediaid']);
  75.             $log->setSources(['items'=>array_unique($videos)]);
  76.             //check si on a regardé les vidéos principales
  77.             if($this->formationService->formationViewedMain($data["entityId"],array_unique($videos))){
  78.                 //on set viewed = 1
  79.                 $f_r $this->em->getRepository(FormationRegistration::class)->findOneBy(["formation"=>$data["entityId"],"User"=>$user]);
  80.                 $f_r->setViewed(1);
  81.                 $f_r->setViewedAt(  new \DateTime("now", new \DateTimeZone("Europe/Paris")) );
  82.                 $this->em->persist($f_r);
  83.                 $log->setSources(['items'=> [ $videos ]]);
  84.                 //update event types
  85.                 $log->setEvent(Events::VIDEO_FORMATION_COMPLETE);
  86.             }
  87.             $this->em->persist($log);
  88.         }
  89.         else{
  90.             $log =  new Log();
  91.             $log->setUser$user );
  92.             $log->setEvent($data['event']);
  93.             $log->setEntity($data['entity']??null);
  94.             $log->setEntityId($data['entityId']);
  95.             if($this->formationService->formationViewedMain($data["entityId"],array_unique([ $data['mediaid'] ]))){
  96.                 $f_r $this->em->getRepository(FormationRegistration::class)->findOneBy(["formation"=>$data["entityId"],"User"=>$user]);
  97.                 $f_r->setViewed(1);
  98.                 $f_r->setViewedAt(  new \DateTime("now", new \DateTimeZone("Europe/Paris")) );
  99.                 $this->em->persist($f_r);
  100.                 $log->setEvent(Events::VIDEO_FORMATION_COMPLETE);
  101.             }
  102.             $log->setSources(['items'=> [ $data['mediaid'] ]]);
  103.             $this->em->persist($log);
  104.         }
  105.         $this->em->flush();
  106.     }
  107. }