Skip to content

Commit

Permalink
refactor: Some constructor args refactoring, remove direct dependency…
Browse files Browse the repository at this point in the history
… on KernelInterface
  • Loading branch information
ambroisemaupate committed Mar 8, 2024
1 parent 032e68e commit de5356f
Show file tree
Hide file tree
Showing 29 changed files with 163 additions and 381 deletions.
1 change: 1 addition & 0 deletions lib/RoadizCoreBundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ services:
$kernelProjectDir: '%kernel.project_dir%'
$apiResourcesDir: '%kernel.project_dir%/config/api_resources'
$debug: '%kernel.debug%'
$kernelEnvironment: '%kernel.environment%'
$defaultControllerClass: '%roadiz_core.default_node_source_controller%'
$defaultLocale: '%kernel.default_locale%'
$webhookMessageTypes: '%roadiz_core.webhook.message_types%'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,17 @@
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Subscribe to Node, NodesSources and UrlAlias event to clear ns url cache.
*/
class NodeRedirectionSubscriber implements EventSubscriberInterface
{
protected NodeMover $nodeMover;
protected KernelInterface $kernel;
protected PreviewResolverInterface $previewResolver;

/**
* @param NodeMover $nodeMover
* @param KernelInterface $kernel
* @param PreviewResolverInterface $previewResolver
*/
public function __construct(
NodeMover $nodeMover,
KernelInterface $kernel,
PreviewResolverInterface $previewResolver
protected readonly NodeMover $nodeMover,
protected readonly string $kernelEnvironment,
protected readonly PreviewResolverInterface $previewResolver
) {
$this->nodeMover = $nodeMover;
$this->kernel = $kernel;
$this->previewResolver = $previewResolver;
}

public static function getSubscribedEvents(): array
Expand All @@ -56,7 +43,7 @@ public function redirectOldPaths(
EventDispatcherInterface $dispatcher
): void {
if (
$this->kernel->getEnvironment() === 'prod' &&
$this->kernelEnvironment === 'prod' &&
!$this->previewResolver->isPreview() &&
null !== $event->getNode() &&
$event->getNode()->isPublished() &&
Expand Down
1 change: 1 addition & 0 deletions lib/RoadizRozierBundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
$addNodeFormTypeClass: '%roadiz_rozier.add_node_form.class%'
$googleServerId: '%roadiz_core.medias.google_server_id%'
$soundcloudClientId: '%roadiz_core.medias.soundcloud_client_id%'
$allowNodeTypeEdition: '%kernel.debug%'

RZ\Roadiz\RozierBundle\:
resource: '../src/'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,15 @@

class AttributeController extends AbstractAdminWithBulkController
{
private AttributeImporter $attributeImporter;

public function __construct(
AttributeImporter $attributeImporter,
private readonly AttributeImporter $attributeImporter,
FormFactoryInterface $formFactory,
SerializerInterface $serializer,
UrlGeneratorInterface $urlGenerator
) {
parent::__construct($formFactory, $serializer, $urlGenerator);
$this->attributeImporter = $attributeImporter;
}


/**
* @inheritDoc
*/
Expand Down
9 changes: 2 additions & 7 deletions lib/Rozier/src/Controllers/CacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@

final class CacheController extends RozierApp
{
private LoggerInterface $logger;
private CacheClearerInterface $cacheClearer;

public function __construct(
CacheClearerInterface $cacheClearer,
LoggerInterface $logger
private readonly CacheClearerInterface $cacheClearer,
private readonly LoggerInterface $logger
) {
$this->logger = $logger;
$this->cacheClearer = $cacheClearer;
}

public function deleteDoctrineCache(Request $request): Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@

class CustomFormsUtilsController extends RozierApp
{
private CustomFormAnswerSerializer $customFormAnswerSerializer;

/**
* @param CustomFormAnswerSerializer $customFormAnswerSerializer
*/
public function __construct(CustomFormAnswerSerializer $customFormAnswerSerializer)
public function __construct(private readonly CustomFormAnswerSerializer $customFormAnswerSerializer)
{
$this->customFormAnswerSerializer = $customFormAnswerSerializer;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use RZ\Roadiz\CoreBundle\Entity\Translation;
use RZ\Roadiz\CoreBundle\Event\Document\DocumentTranslationUpdatedEvent;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
Expand All @@ -34,7 +35,7 @@ class DocumentTranslationsController extends RozierApp
* @return Response
* @throws RuntimeError
*/
public function editAction(Request $request, int $documentId, ?int $translationId = null)
public function editAction(Request $request, int $documentId, ?int $translationId = null): Response
{
$this->denyAccessUnlessGranted('ROLE_ACCESS_DOCUMENTS');

Expand Down Expand Up @@ -117,14 +118,10 @@ public function editAction(Request $request, int $documentId, ?int $translationI
throw new ResourceNotFoundException();
}

/**
* @param Document $document
* @param TranslationInterface $translation
*
* @return DocumentTranslation
*/
protected function createDocumentTranslation(Document $document, TranslationInterface $translation)
{
protected function createDocumentTranslation(
Document $document,
TranslationInterface $translation
): DocumentTranslation {
$dt = new DocumentTranslation();
$dt->setDocument($document);
$dt->setTranslation($translation);
Expand All @@ -144,7 +141,7 @@ protected function createDocumentTranslation(Document $document, TranslationInte
* @return Response
* @throws RuntimeError
*/
public function deleteAction(Request $request, int $documentId, int $translationId)
public function deleteAction(Request $request, int $documentId, int $translationId): Response
{
$this->denyAccessUnlessGranted('ROLE_ACCESS_DOCUMENTS_DELETE');

Expand Down Expand Up @@ -201,12 +198,7 @@ public function deleteAction(Request $request, int $documentId, int $translation
throw new ResourceNotFoundException();
}

/**
* @param DocumentTranslation $doc
*
* @return \Symfony\Component\Form\FormInterface
*/
private function buildDeleteForm(DocumentTranslation $doc)
private function buildDeleteForm(DocumentTranslation $doc): FormInterface
{
$defaults = [
'documentTranslationId' => $doc->getId(),
Expand Down Expand Up @@ -240,11 +232,6 @@ protected function onPostUpdate(PersistableInterface $entity, Request $request):
}
}

/**
* @param PersistableInterface $entity
*
* @return Response
*/
protected function getPostUpdateRedirection(PersistableInterface $entity): ?Response
{
if (
Expand Down
49 changes: 12 additions & 37 deletions lib/Rozier/src/Controllers/Documents/DocumentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@

class DocumentsController extends RozierApp
{
private array $documentPlatforms;
private DocumentFactory $documentFactory;
private HandlerFactoryInterface $handlerFactory;
private LoggerInterface $logger;
private RandomImageFinder $randomImageFinder;
private RendererInterface $renderer;
private DocumentUrlGeneratorInterface $documentUrlGenerator;
private UrlGeneratorInterface $urlGenerator;
private FilesystemOperator $documentsStorage;
private ?string $googleServerId;
private ?string $soundcloudClientId;

protected array $thumbnailFormat = [
'quality' => 50,
'fit' => '128x128',
Expand All @@ -81,34 +69,21 @@ class DocumentsController extends RozierApp
'controls' => false,
'loading' => 'lazy',
];
private EmbedFinderFactory $embedFinderFactory;

public function __construct(
array $documentPlatforms,
FilesystemOperator $documentsStorage,
HandlerFactoryInterface $handlerFactory,
LoggerInterface $logger,
RandomImageFinder $randomImageFinder,
DocumentFactory $documentFactory,
RendererInterface $renderer,
DocumentUrlGeneratorInterface $documentUrlGenerator,
UrlGeneratorInterface $urlGenerator,
EmbedFinderFactory $embedFinderFactory,
?string $googleServerId = null,
?string $soundcloudClientId = null
private readonly array $documentPlatforms,
private readonly FilesystemOperator $documentsStorage,
private readonly HandlerFactoryInterface $handlerFactory,
private readonly LoggerInterface $logger,
private readonly RandomImageFinder $randomImageFinder,
private readonly DocumentFactory $documentFactory,
private readonly RendererInterface $renderer,
private readonly DocumentUrlGeneratorInterface $documentUrlGenerator,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly EmbedFinderFactory $embedFinderFactory,
private readonly ?string $googleServerId = null,
private readonly ?string $soundcloudClientId = null
) {
$this->documentPlatforms = $documentPlatforms;
$this->handlerFactory = $handlerFactory;
$this->logger = $logger;
$this->randomImageFinder = $randomImageFinder;
$this->documentFactory = $documentFactory;
$this->renderer = $renderer;
$this->documentUrlGenerator = $documentUrlGenerator;
$this->urlGenerator = $urlGenerator;
$this->googleServerId = $googleServerId;
$this->soundcloudClientId = $soundcloudClientId;
$this->documentsStorage = $documentsStorage;
$this->embedFinderFactory = $embedFinderFactory;
}

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

class FoldersController extends RozierApp
{
private DocumentArchiver $documentArchiver;

public function __construct(DocumentArchiver $documentArchiver)
public function __construct(private readonly DocumentArchiver $documentArchiver)
{
$this->documentArchiver = $documentArchiver;
}

public function indexAction(Request $request): Response
Expand Down
15 changes: 4 additions & 11 deletions lib/Rozier/src/Controllers/GroupsUtilsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,10 @@

class GroupsUtilsController extends RozierApp
{
private SerializerInterface $serializer;
private GroupsImporter $groupsImporter;

/**
* @param SerializerInterface $serializer
* @param GroupsImporter $groupsImporter
*/
public function __construct(SerializerInterface $serializer, GroupsImporter $groupsImporter)
{
$this->serializer = $serializer;
$this->groupsImporter = $groupsImporter;
public function __construct(
private readonly SerializerInterface $serializer,
private readonly GroupsImporter $groupsImporter
) {
}

/**
Expand Down
20 changes: 8 additions & 12 deletions lib/Rozier/src/Controllers/NodeTypeFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
Expand All @@ -22,13 +21,10 @@

class NodeTypeFieldsController extends RozierApp
{
private MessageBusInterface $messageBus;
private KernelInterface $kernel;

public function __construct(KernelInterface $kernel, MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
$this->kernel = $kernel;
public function __construct(
private readonly bool $allowNodeTypeEdition,
private readonly MessageBusInterface $messageBus
) {
}

/**
Expand Down Expand Up @@ -82,7 +78,7 @@ public function editAction(Request $request, int $nodeTypeFieldId): Response
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if (!$this->kernel->isDebug()) {
if (!$this->allowNodeTypeEdition) {
$form->addError(new FormError('You cannot edit node-type fields in production.'));
} else {
$this->em()->flush();
Expand Down Expand Up @@ -138,12 +134,12 @@ public function addAction(Request $request, int $nodeTypeId): Response
$this->assignation['field'] = $field;

$form = $this->createForm(NodeTypeFieldType::class, $field, [
'disabled' => !$this->kernel->isDebug()
'disabled' => !$this->allowNodeTypeEdition
]);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if (!$this->kernel->isDebug()) {
if (!$this->allowNodeTypeEdition) {
$form->addError(new FormError('You cannot add node-type fields in production.'));
} else {
try {
Expand Down Expand Up @@ -198,7 +194,7 @@ public function deleteAction(Request $request, int $nodeTypeFieldId): Response
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if (!$this->kernel->isDebug()) {
if (!$this->allowNodeTypeEdition) {
$form->addError(new FormError('You cannot delete node-type fields in production.'));
} else {
/** @var NodeType $nodeType */
Expand Down
18 changes: 7 additions & 11 deletions lib/Rozier/src/Controllers/NodeTypes/NodeTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Themes\Rozier\Forms\NodeTypeType;
Expand All @@ -22,13 +21,10 @@

class NodeTypesController extends RozierApp
{
private MessageBusInterface $messageBus;
private KernelInterface $kernel;

public function __construct(KernelInterface $kernel, MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
$this->kernel = $kernel;
public function __construct(
private readonly bool $allowNodeTypeEdition,
private readonly MessageBusInterface $messageBus
) {
}

public function indexAction(Request $request): Response
Expand Down Expand Up @@ -112,12 +108,12 @@ public function addAction(Request $request): Response
$nodeType = new NodeType();

$form = $this->createForm(NodeTypeType::class, $nodeType, [
'disabled' => !$this->kernel->isDebug()
'disabled' => !$this->allowNodeTypeEdition
]);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if (!$this->kernel->isDebug()) {
if (!$this->allowNodeTypeEdition) {
$form->addError(new FormError('You cannot create a node-type in production mode.'));
} else {
try {
Expand Down Expand Up @@ -166,7 +162,7 @@ public function deleteAction(Request $request, int $nodeTypeId): Response
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if (!$this->kernel->isDebug()) {
if (!$this->allowNodeTypeEdition) {
$form->addError(new FormError('You cannot delete a node-type in production mode.'));
} else {
$this->messageBus->dispatch(new Envelope(new DeleteNodeTypeMessage($nodeType->getId())));
Expand Down
Loading

0 comments on commit de5356f

Please sign in to comment.