<?php
namespace App\Controller\Customer;
use App\DataProvider\Module\ModuleItemDataProvider;
use App\Model\Customer\Customer;
use App\Model\Exception\ModuleNotFoundException;
use App\Model\Module\Module;
use App\Model\ModuleConfiguration\ModuleConfiguration;
use App\Service\Cache\CacheManager;
use App\Service\Customer\CustomerModuleManager;
use Doctrine\Common\Annotations\AnnotationException;
use ImagickException;
use ReflectionException;
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
final class UpdateModuleAccessAction
{
/**
* @param Customer $data
* @param Request $request
* @param CustomerModuleManager $customerModuleManager
* @param ModuleItemDataProvider $moduleItemDataProvider
* @param CacheManager $cacheManager
*
* @return Response
*
* @throws AnnotationException
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws ExceptionInterface
* @throws ImagickException
* @throws ModuleNotFoundException
* @throws RedirectionExceptionInterface
* @throws ReflectionException
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws \Psr\Cache\InvalidArgumentException
*/
public function __invoke(
Customer $data,
Request $request,
CustomerModuleManager $customerModuleManager,
ModuleItemDataProvider $moduleItemDataProvider,
CacheManager $cacheManager
): Response {
$content = json_decode($request->getContent(), true);
if (empty($content['moduleId'])) {
throw new InvalidArgumentException('Aucun module spécifié pour mise à jour des droits');
}
$response = $customerModuleManager->enableOrDisableModuleForCustomer(
$moduleItemDataProvider->getItem(
Module::class,
$content['moduleId']
),
$data
);
$cacheManager->invalidateTag([$cacheManager->getCustomerPrefix().'_'.ModuleConfiguration::class]);
$cacheManager->invalidateTag([$cacheManager->getCustomerPrefix().'_'.Module::class]);
return new JsonResponse($response, Response::HTTP_OK);
}
}