Skip to content

Commit

Permalink
refactor: Use constructor readonly initializers on Ajax controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Mar 7, 2024
1 parent 87878c2 commit ce01609
Show file tree
Hide file tree
Showing 20 changed files with 62 additions and 194 deletions.
10 changes: 2 additions & 8 deletions lib/Rozier/src/AjaxControllers/AjaxAbstractFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@

abstract class AjaxAbstractFieldsController extends AbstractAjaxController
{
private HandlerFactoryInterface $handlerFactory;

/**
* @param HandlerFactoryInterface $handlerFactory
*/
public function __construct(HandlerFactoryInterface $handlerFactory)
public function __construct(protected readonly HandlerFactoryInterface $handlerFactory)
{
$this->handlerFactory = $handlerFactory;
}

/**
Expand All @@ -30,7 +24,7 @@ public function __construct(HandlerFactoryInterface $handlerFactory)
*
* @return null|Response
*/
protected function handleFieldActions(Request $request, AbstractField $field = null)
protected function handleFieldActions(Request $request, AbstractField $field = null): ?Response
{
/*
* Validate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ public function editAction(Request $request, int $attributeValueId): Response
);
}

/**
* @param array $parameters
* @param AttributeValue $attributeValue
*
* @return array
*/
protected function updatePosition($parameters, AttributeValue $attributeValue): array
protected function updatePosition(array $parameters, AttributeValue $attributeValue): array
{
$attributable = $attributeValue->getAttributable();
$details = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class AjaxCustomFormFieldsController extends AjaxAbstractFieldsController
* such as coming from widgets.
*
* @param Request $request
* @param int $customFormFieldId
* @param int $customFormFieldId
*
* @return Response JSON response
*/
public function editAction(Request $request, int $customFormFieldId)
public function editAction(Request $request, int $customFormFieldId): Response
{
/*
* Validate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@

class AjaxCustomFormsExplorerController extends AbstractAjaxController
{
private UrlGeneratorInterface $urlGenerator;

public function __construct(UrlGeneratorInterface $urlGenerator)
public function __construct(private readonly UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}

/**
Expand Down Expand Up @@ -108,7 +105,7 @@ public function listAction(Request $request): Response
* @param array<CustomForm>|\Traversable<CustomForm> $customForms
* @return array
*/
private function normalizeCustomForms($customForms)
private function normalizeCustomForms(iterable $customForms): array
{
$customFormsArray = [];

Expand Down
28 changes: 7 additions & 21 deletions lib/Rozier/src/AjaxControllers/AjaxDocumentsExplorerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,27 @@
use RZ\Roadiz\Documents\UrlGenerators\DocumentUrlGeneratorInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Themes\Rozier\Models\DocumentModel;

class AjaxDocumentsExplorerController extends AbstractAjaxController
{
private RendererInterface $renderer;
private DocumentUrlGeneratorInterface $documentUrlGenerator;
private UrlGeneratorInterface $urlGenerator;
private EmbedFinderFactory $embedFinderFactory;

public function __construct(
RendererInterface $renderer,
DocumentUrlGeneratorInterface $documentUrlGenerator,
UrlGeneratorInterface $urlGenerator,
EmbedFinderFactory $embedFinderFactory
private readonly RendererInterface $renderer,
private readonly DocumentUrlGeneratorInterface $documentUrlGenerator,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly EmbedFinderFactory $embedFinderFactory
) {
$this->renderer = $renderer;
$this->documentUrlGenerator = $documentUrlGenerator;
$this->urlGenerator = $urlGenerator;
$this->embedFinderFactory = $embedFinderFactory;
}

public static array $thumbnailArray = [
"fit" => "40x40",
"quality" => 50,
"inline" => false,
];
/**
* @param Request $request
*
* @return Response JSON response
*/
public function indexAction(Request $request)

public function indexAction(Request $request): JsonResponse
{
$this->denyAccessUnlessGranted('ROLE_ACCESS_DOCUMENTS');

Expand Down Expand Up @@ -108,7 +94,7 @@ public function indexAction(Request $request)
* @param Request $request
* @return JsonResponse
*/
public function listAction(Request $request)
public function listAction(Request $request): JsonResponse
{
$this->denyAccessUnlessGranted('ROLE_ACCESS_DOCUMENTS');

Expand Down
23 changes: 4 additions & 19 deletions lib/Rozier/src/AjaxControllers/AjaxEntitiesExplorerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,12 @@

class AjaxEntitiesExplorerController extends AbstractAjaxController
{
private RendererInterface $renderer;
private DocumentUrlGeneratorInterface $documentUrlGenerator;
private UrlGeneratorInterface $urlGenerator;
private EmbedFinderFactory $embedFinderFactory;

public function __construct(
RendererInterface $renderer,
DocumentUrlGeneratorInterface $documentUrlGenerator,
UrlGeneratorInterface $urlGenerator,
EmbedFinderFactory $embedFinderFactory
private readonly RendererInterface $renderer,
private readonly DocumentUrlGeneratorInterface $documentUrlGenerator,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly EmbedFinderFactory $embedFinderFactory
) {
$this->renderer = $renderer;
$this->documentUrlGenerator = $documentUrlGenerator;
$this->urlGenerator = $urlGenerator;
$this->embedFinderFactory = $embedFinderFactory;
}

/**
Expand Down Expand Up @@ -118,12 +109,6 @@ public function indexAction(Request $request): JsonResponse
);
}

/**
* Get a Node list from an array of id.
*
* @param Request $request
* @return JsonResponse
*/
public function listAction(Request $request): JsonResponse
{
if (!$request->query->has('nodeTypeFieldId')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@

class AjaxExplorerProviderController extends AbstractAjaxController
{
private ContainerInterface $psrContainer;

public function __construct(ContainerInterface $psrContainer)
public function __construct(private readonly ContainerInterface $psrContainer)
{
$this->psrContainer = $psrContainer;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions lib/Rozier/src/AjaxControllers/AjaxFolderTreeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@

class AjaxFolderTreeController extends AbstractAjaxController
{
private TreeWidgetFactory $treeWidgetFactory;

public function __construct(TreeWidgetFactory $treeWidgetFactory)
public function __construct(private readonly TreeWidgetFactory $treeWidgetFactory)
{
$this->treeWidgetFactory = $treeWidgetFactory;
}

public function getTreeAction(Request $request): JsonResponse
Expand Down
22 changes: 5 additions & 17 deletions lib/Rozier/src/AjaxControllers/AjaxFoldersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,15 @@

class AjaxFoldersController extends AbstractAjaxController
{
private HandlerFactoryInterface $handlerFactory;

public function __construct(HandlerFactoryInterface $handlerFactory)
public function __construct(private readonly HandlerFactoryInterface $handlerFactory)
{
$this->handlerFactory = $handlerFactory;
}

/**
/*
* Handle AJAX edition requests for Folder
* such as coming from tag-tree widgets.
*
* @param Request $request
* @param int $folderId
*
* @return Response JSON response
*/
public function editAction(Request $request, int $folderId)
public function editAction(Request $request, int $folderId): JsonResponse
{
$this->validateRequest($request);
$this->denyAccessUnlessGranted('ROLE_ACCESS_DOCUMENTS');
Expand Down Expand Up @@ -81,7 +73,7 @@ public function editAction(Request $request, int $folderId)
* @param Request $request
* @return JsonResponse
*/
public function searchAction(Request $request)
public function searchAction(Request $request): JsonResponse
{
$this->denyAccessUnlessGranted('ROLE_ACCESS_DOCUMENTS');

Expand Down Expand Up @@ -111,11 +103,7 @@ public function searchAction(Request $request)
throw $this->createNotFoundException($this->getTranslator()->trans('no.folder.found'));
}

/**
* @param array $parameters
* @param Folder $folder
*/
protected function updatePosition($parameters, Folder $folder): void
protected function updatePosition(array $parameters, Folder $folder): void
{
/*
* First, we set the new parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@

class AjaxFoldersExplorerController extends AbstractAjaxController
{
/**
* @param Request $request
*
* @return Response JSON response
*/
public function indexAction(Request $request)
public function indexAction(Request $request): JsonResponse
{
$this->denyAccessUnlessGranted('ROLE_ACCESS_DOCUMENTS');

Expand Down
2 changes: 1 addition & 1 deletion lib/Rozier/src/AjaxControllers/AjaxNodeTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function listAction(Request $request): Response
* @param array<NodeType>|\Traversable<NodeType> $nodeTypes
* @return array
*/
private function normalizeNodeType($nodeTypes)
private function normalizeNodeType(iterable $nodeTypes): array
{
$nodeTypesArray = [];

Expand Down
30 changes: 7 additions & 23 deletions lib/Rozier/src/AjaxControllers/AjaxNodesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Themes\Rozier\AjaxControllers;

use Psr\Log\LoggerInterface;
use RZ\Roadiz\CoreBundle\Security\Authorization\Chroot\NodeChrootResolver;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\Tag;
use RZ\Roadiz\CoreBundle\Event\Node\NodeCreatedEvent;
Expand All @@ -19,6 +18,7 @@
use RZ\Roadiz\CoreBundle\Node\NodeMover;
use RZ\Roadiz\CoreBundle\Node\NodeNamePolicyInterface;
use RZ\Roadiz\CoreBundle\Node\UniqueNodeGenerator;
use RZ\Roadiz\CoreBundle\Security\Authorization\Chroot\NodeChrootResolver;
use RZ\Roadiz\CoreBundle\Security\Authorization\Voter\NodeVoter;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -29,27 +29,14 @@

class AjaxNodesController extends AbstractAjaxController
{
private NodeNamePolicyInterface $nodeNamePolicy;
private LoggerInterface $logger;
private NodeMover $nodeMover;
private NodeChrootResolver $nodeChrootResolver;
private Registry $workflowRegistry;
private UniqueNodeGenerator $uniqueNodeGenerator;

public function __construct(
NodeNamePolicyInterface $nodeNamePolicy,
LoggerInterface $logger,
NodeMover $nodeMover,
NodeChrootResolver $nodeChrootResolver,
Registry $workflowRegistry,
UniqueNodeGenerator $uniqueNodeGenerator
private readonly NodeNamePolicyInterface $nodeNamePolicy,
private readonly LoggerInterface $logger,
private readonly NodeMover $nodeMover,
private readonly NodeChrootResolver $nodeChrootResolver,
private readonly Registry $workflowRegistry,
private readonly UniqueNodeGenerator $uniqueNodeGenerator
) {
$this->nodeNamePolicy = $nodeNamePolicy;
$this->logger = $logger;
$this->nodeMover = $nodeMover;
$this->nodeChrootResolver = $nodeChrootResolver;
$this->workflowRegistry = $workflowRegistry;
$this->uniqueNodeGenerator = $uniqueNodeGenerator;
}

/**
Expand Down Expand Up @@ -248,10 +235,7 @@ protected function parsePosition(array $parameters, float $default = 0.0): float
* Update node's status.
*
* @param Request $request
*
* @return JsonResponse
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function statusesAction(Request $request): JsonResponse
{
Expand Down
25 changes: 6 additions & 19 deletions lib/Rozier/src/AjaxControllers/AjaxNodesExplorerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,14 @@

final class AjaxNodesExplorerController extends AbstractAjaxController
{
private SerializerInterface $serializer;
private ClientRegistry $clientRegistry;
private NodeSourceSearchHandlerInterface $nodeSourceSearchHandler;
private NodeTypeApi $nodeTypeApi;
private UrlGeneratorInterface $urlGenerator;
private Security $security;

public function __construct(
SerializerInterface $serializer,
ClientRegistry $clientRegistry,
NodeSourceSearchHandlerInterface $nodeSourceSearchHandler,
NodeTypeApi $nodeTypeApi,
UrlGeneratorInterface $urlGenerator,
Security $security,
private readonly SerializerInterface $serializer,
private readonly ClientRegistry $clientRegistry,
private readonly NodeSourceSearchHandlerInterface $nodeSourceSearchHandler,
private readonly NodeTypeApi $nodeTypeApi,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly Security $security,
) {
$this->nodeSourceSearchHandler = $nodeSourceSearchHandler;
$this->nodeTypeApi = $nodeTypeApi;
$this->serializer = $serializer;
$this->urlGenerator = $urlGenerator;
$this->clientRegistry = $clientRegistry;
$this->security = $security;
}

protected function getItemPerPage(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class AjaxSearchNodesSourcesController extends AbstractAjaxController
public const RESULT_COUNT = 10;

public function __construct(
private DocumentUrlGeneratorInterface $documentUrlGenerator,
private Security $security
private readonly DocumentUrlGeneratorInterface $documentUrlGenerator,
private readonly Security $security
) {
}

Expand Down
11 changes: 4 additions & 7 deletions lib/Rozier/src/AjaxControllers/AjaxTagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@

class AjaxTagsController extends AbstractAjaxController
{
private HandlerFactoryInterface $handlerFactory;
private UrlGeneratorInterface $urlGenerator;

public function __construct(HandlerFactoryInterface $handlerFactory, UrlGeneratorInterface $urlGenerator)
{
$this->handlerFactory = $handlerFactory;
$this->urlGenerator = $urlGenerator;
public function __construct(
private readonly HandlerFactoryInterface $handlerFactory,
private readonly UrlGeneratorInterface $urlGenerator
) {
}

/**
Expand Down
Loading

0 comments on commit ce01609

Please sign in to comment.