<?php
namespace LaunchPad\Bundle\LaunchPadBundle\Base\EventListener;
use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\ORM\EntityManagerInterface;
use LaunchPad\Bundle\LaunchPadBundle\Base\Serializer\IdCircularReferenceHandler;
use LaunchPad\Bundle\LaunchPadBundle\Base\Service\Exception\MissingApiParamException;
use LaunchPad\Bundle\LaunchPadBundle\Base\Service\User\UserPermissionService;
use LaunchPad\Bundle\LaunchPadBundle\Base\Service\User\UserActionService;
use LaunchPad\Bundle\LaunchPadBundle\Base\Service\User\UserSessionService;
use LaunchPad\Bundle\LaunchPadBundle\Base\Service\UserDeviceService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use LaunchPad\Bundle\LaunchPadBundle\Api\Controller\BaseApiController;
use LaunchPad\Bundle\LaunchPadBundle\Base\Service\BaseService;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
class AppRequestListener
{
/**
* @var BaseService
*/
private $apiService;
/**
* @var ContainerInterface
*/
private $container;
/**
* @var UserPermissionService
*/
public $userPermissionService;
/**
* @var UserActionService
*/
private $userActionService;
/**
* @var UserSessionService
*/
private $userSessionService;
/**
* @var AuthorizationCheckerInterface
*/
private $authChecker;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var UserDeviceService
*/
private $userDeviceService;
/**
* @param ContainerInterface $container
* @param BaseService $apiService
* @param UserActionService $userActionService
* @param UserSessionService $userSessionService
* @param AuthorizationCheckerInterface $authChecker
* @param UserPermissionService $userPermissionService
* @throws AnnotationException
*/
public function __construct(
ContainerInterface $container,
BaseService $apiService,
UserActionService $userActionService,
UserSessionService $userSessionService,
AuthorizationCheckerInterface $authChecker,
UserPermissionService $userPermissionService
) {
$this->apiService = $apiService;
$this->container = $container;
$this->userPermissionService = $userPermissionService;
$this->userActionService = $userActionService;
$this->userSessionService = $userSessionService;
$this->authChecker = $authChecker;
$this->userDeviceService = $this->container->get('lp.user.device');
$this->serializer = new Serializer([
new DateTimeNormalizer(),
new ObjectNormalizer(
new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())),
null,
null,
null,
null,
null,
[
ObjectNormalizer::CIRCULAR_REFERENCE_HANDLER => new IdCircularReferenceHandler(),
]
),
new GetSetMethodNormalizer(),
], [
new JsonEncoder(),
]);
}
/**
* Return JSON output
*
* @param $data
* @param int $code
* @param array $groups
* @return JsonResponse
* @throws ExceptionInterface
*/
private function jsonOutput($data, $code = 200, $groups = [])
{
return new JsonResponse(
$this->serializer->serialize(
$groups
? $this->serializer->normalize($data, 'json', ['groups' => $groups])
: $data,
'json',
['groups' => $groups]
),
$code,
[],
true
);
}
/**
* @param ControllerEvent $event
* @throws MissingApiParamException
* @throws \Exception
*/
public function onKernelController(ControllerEvent $event): void
{
// die('test');
$permissionRoute = str_ireplace('/', '.', $event->getRequest()->getRequestUri());
if (strpos($permissionRoute, '.') === 0) {
$permissionRoute = substr($permissionRoute, 1);
}
// $this->userPermissionService->checkPermission($permissionRoute);
$controller = $event->getController();
if (!is_array($controller)) {
return;
}
$controllerName = explode('\\', get_class($controller[0]));
$controllerName = lcfirst(substr(end($controllerName), 0, -10));
// Check controller type
if (!($controller[0] instanceof BaseApiController)) {
return;
}
// Get controller action
$controllerAction = $controller[1];
if (substr($controllerAction, -6) === 'Action') {
$controllerAction = substr($controllerAction, 0, -6);
}
// Check mandatory params
$controller[0]->setData($this->apiService->extractParams($event->getRequest()));
if($this->authChecker->isGranted('ROLE_USER')){
$this->userActionService->newUserAction(
$this->userSessionService->makeNewSession(),
"{$controllerName}.{$controllerAction}"
);
}
$this->userDeviceService->handleDevice();
}
}