<?php
namespace App\EventSubscriber;
use App\Entity\FormationRegistration;
use App\Entity\Log;
use App\Events\Events;
use App\Service\Algo\JwpUtils;
use App\Service\formation\FormationService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class LogJwpEventsSubscriber implements EventSubscriberInterface
{
private $em;
/**
* @var TokenStorageInterface
*/
private $token;
/**
* @var FormationService
*/
private $formationService;
private JwpUtils $jwpUtils;
/**
* LogJwpEventsSubscriber constructor.
* @param $em
*/
public function __construct(EntityManagerInterface $em, TokenStorageInterface $token, FormationService $formationService, JwpUtils $jwpUtils)
{
$this->em = $em;
$this->token = $token;
$this->formationService = $formationService;
$this->jwpUtils = $jwpUtils;
}
public static function getSubscribedEvents()
{
return [
Events::VIDEO_COMPLETE => 'onVideoComplete',
Events::VIDEO_FORMATION_PROGRESS => 'onVideoFormationProgress',
];
}
public function onVideoComplete(GenericEvent $event): void
{
$data = $event->getSubject();
$user = $this->token->getToken()->getUser();
$sources = $data['source'];
$metadata = $sources['feedData'];
if ($metadata['CAT'] === 'ERGO'){
$sources['visibility'] = $this->jwpUtils->setVisibilitySource($user,$metadata);
}
$log = new Log();
$log->setUser( $user );
$log->setEvent($data['event']);
$log->setEntityId($data['mediaid']);
$log->setSources($sources);
$this->em->persist($log);
$this->em->flush();
}
public function onVideoFormationProgress(GenericEvent $event): void
{
$data = $event->getSubject();
$user = $this->token->getToken()->getUser();
/* @var $log Log*/
$log = $this->em->getRepository(Log::class)->findOneBy([
'event'=>Events::VIDEO_FORMATION_PROGRESS,
'entity'=>'formation',
'entityId'=>$data['entityId'],
'user'=>$user
]);
//je trouve un log en progress
if($log){
//update sources
$videos = $log->getSources()['items'] ?? [];
// Normalize logs created by the previous nested-array format.
$videos = array_reduce($videos, function (array $flattenedVideos, $video): array {
return array_merge($flattenedVideos, is_array($video) ? $video : [$video]);
}, []);
$videos[] = $data['mediaid'];
$videos = array_values(array_unique($videos));
$log->setSources(['items' => $videos]);
//check si on a regardé les vidéos principales
if($this->formationService->formationViewedMain($data["entityId"],array_unique($videos))){
//on set viewed = 1
$f_r = $this->em->getRepository(FormationRegistration::class)->findOneBy(["formation"=>$data["entityId"],"User"=>$user]);
$f_r->setViewed(1);
$f_r->setViewedAt( new \DateTime("now", new \DateTimeZone("Europe/Paris")) );
$this->em->persist($f_r);
$log->setSources(['items' => $videos]);
//update event types
$log->setEvent(Events::VIDEO_FORMATION_COMPLETE);
}
$this->em->persist($log);
}
else
{
$log = new Log();
$log->setUser( $user );
$log->setEvent($data['event']);
$log->setEntity($data['entity']??null);
$log->setEntityId($data['entityId']);
if($this->formationService->formationViewedMain($data["entityId"],array_unique([ $data['mediaid'] ]))){
$f_r = $this->em->getRepository(FormationRegistration::class)->findOneBy(["formation"=>$data["entityId"],"User"=>$user]);
$f_r->setViewed(1);
$f_r->setViewedAt( new \DateTime("now", new \DateTimeZone("Europe/Paris")) );
$this->em->persist($f_r);
$log->setEvent(Events::VIDEO_FORMATION_COMPLETE);
}
$log->setSources(['items' => [$data['mediaid']]]);
$this->em->persist($log);
}
$this->em->flush();
}
}