<?php
/**
* Created by PhpStorm.
* User: SUSAN MEDINA
* Date: 24/04/2019
* Time: 01:31 PM
*/
namespace App\EventSubscriber\Resource;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Request;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Exception\InvalidArgumentException;
use App\Exception\NotFoundException;
use App\Repository\App\MediaObjectRepository;
use App\Repository\App\ResourceRepository;
use App\Services\VendorService;
use App\Services\UtilsService;
use App\Entity\App\ResourceAndTool;
use App\Entity\App\MediaObject;
use App\Entity\App\Category;
use App\Entity\App\User;
class ResourcePostValidateSubscriber implements EventSubscriberInterface
{
private $tokenStorage;
private $request;
private $mediaObjectRepository;
private $resourceRepository;
private $utilsService;
private $vendorService;
private $translator;
public function __construct(
MediaObjectRepository $mediaObjectRepository,
ResourceRepository $resourceRepository,
TokenStorageInterface $tokenStorage,
UtilsService $utilsService,
VendorService $vendorService,
TranslatorInterface $translator
){
$this->tokenStorage = $tokenStorage;
$this->mediaObjectRepository = $mediaObjectRepository;
$this->resourceRepository = $resourceRepository;
$this->utilsService = $utilsService;
$this->vendorService = $vendorService;
$this->translator = $translator;
}
/**
* @param ViewEvent $event
* @throws InvalidArgumentException
* @throws NotFoundException
*/
public function onKernelView(ViewEvent $event)
{
if ($this->utilsService->isAPublicRequest($event)) {
return;
}
$resource = $event->getControllerResult();
$this->request = $event->getRequest();
$method = $this->request->getMethod();
$userCurrent = $this->tokenStorage->getToken()->getUser();
if (!($resource instanceof ResourceAndTool) ||
(Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method)
)
return;
if(!($userCurrent instanceof User))
throw new NotFoundException($this->translator->trans('User current not found'));
$vendor = $resource->getVendor();
if($resource->getCategory()) {
if ($resource->getCategory()->getVendor() !== $vendor) {
throw new InvalidArgumentException(
$this->translator->trans('The category does not belongs to the vendor',
['%vendor%' => $vendor->getName()]
)
);
}
if ($resource->getCategory()->getType() !== Category::TYPE_RESOURCE) {
throw new InvalidArgumentException(
$this->translator->trans('category.validate.type_not_allowed',
[
'%type%' => $resource->getCategory()->getType(),
'%typeAllowed%' => Category::TYPE_RESOURCE
]
)
);
}
}
if ($resource->getLocation()) {
if ($resource->getLocation()->getVendor() !== $vendor) {
throw new InvalidArgumentException(
$this->translator->trans('vendor.validate.does_not_belongs',
[
'%entity%' => $this->translator->trans('location.title'),
'%vendorName%' => $vendor->getName()
]
)
);
}
}
$content = $this->request->getContent();
$params = json_decode($content, true);
if(isset($params['mediaContent']) &&
is_array($params['mediaContent']) &&
count($params['mediaContent']) > 0
){
foreach ($resource->getMediaContent() as $mediaId){
$media = $this->mediaObjectRepository->find($mediaId);
if (!$media instanceof MediaObject) {
throw new NotFoundException(
$this->translator->trans('general.validate.not_exists',
[
'%entityName%' => $this->translator->trans('mediaObject.name'),
'%entityId%' => $mediaId
]
)
);
}
if ($media->getResource() !== null &&
$media->getresource() !== $resource) {
throw new InvalidArgumentException(
$this->translator->trans('mediaObject.validate.does_not_belongs',
[
'%mediaId%' => $mediaId,
'%entity%' => $this->translator->trans('resource.name')
]
)
);
}
if ($media->getType() !== MediaObject::TYPE_RESOURCE) {
throw new InvalidArgumentException(
$this->translator->trans(
'mediaObject.validate.type_not_allowed',
[
'%mediaId%' => $mediaId,
'%mediaType%' => $media->getType(),
'%mediaTypeAvailable%' => MediaObject::TYPE_RESOURCE
]
)
);
}
}
}
if(isset($params['children']) &&
is_array($params['children']) &&
count($params['children']) > 0
){
foreach ($params['children'] as $child) {
if (isset($child['id'])) {
$children = $this->resourceRepository->find($child['id']);
$this->validateParent($children, $resource);
if ($children->getParent() && $children->getParent() !== $resource) {
throw new InvalidArgumentException(
$this->translator->trans(
'This children has belong to another resource already',
['%id%' => $child['id']]
)
);
}
}
}
}
return;
}
/**
* @param ResourceAndTool $resource
* @param ResourceAndTool $parent
* @throws InvalidArgumentException
*/
public function validateParent(ResourceAndTool $resource, ResourceAndTool $parent)
{
if ($resource->getVendor() !== $parent->getVendor()){
throw new InvalidArgumentException(
$this->translator->trans('The vendor between the parent and the new resource is not equal')
);
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['onKernelView', EventPriorities::POST_VALIDATE]
];
}
}