Skip to content

Commit

Permalink
refactor: Deprecated Controller methods to get services
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 15, 2023
1 parent bee2f06 commit d8d351b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 87 deletions.
4 changes: 3 additions & 1 deletion lib/RoadizCompatBundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ services:
public: true
assetPackages:
alias: RZ\Roadiz\Documents\Packages
deprecated: ~
deprecated:
package: 'roadiz/compat-bundle'
version: '2.0.0'
public: true
Symfony\Contracts\Translation\TranslatorInterface:
alias: 'translator.default'
Expand Down
2 changes: 1 addition & 1 deletion lib/RoadizCompatBundle/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ protected function publishMessage(
public function getSession(): ?SessionInterface
{
$request = $this->getRequest();
return null !== $request && $request->hasPreviousSession() ? $request->getSession() : null;
return $request->hasPreviousSession() ? $request->getSession() : null;
}

/**
Expand Down
64 changes: 18 additions & 46 deletions lib/RoadizCompatBundle/src/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public static function getSubscribedServices(): array
'stopwatch' => Stopwatch::class,
'translator' => TranslatorInterface::class,
'urlGenerator' => UrlGeneratorInterface::class,
UrlGeneratorInterface::class => UrlGeneratorInterface::class,
ContactFormManager::class => ContactFormManager::class,
DocumentUrlGeneratorInterface::class => DocumentUrlGeneratorInterface::class,
EmailManager::class => EmailManager::class,
Expand All @@ -103,6 +104,7 @@ public static function getSubscribedServices(): array
Stopwatch::class => Stopwatch::class,
TokenStorageInterface::class => TokenStorageInterface::class,
TranslatorInterface::class => TranslatorInterface::class,
FormFactoryInterface::class => FormFactoryInterface::class,
\RZ\Roadiz\Core\Handlers\HandlerFactoryInterface::class => HandlerFactoryInterface::class,
]);
}
Expand Down Expand Up @@ -205,17 +207,6 @@ protected function getSettingsBag(): Settings
return $settingsBag;
}

/**
* @return Packages
* @deprecated
*/
protected function getPackages(): Packages
{
/** @var Packages $packages */ # php-stan hint
$packages = $this->get('assetPackages');
return $packages;
}

protected function getHandlerFactory(): HandlerFactoryInterface
{
/** @var HandlerFactoryInterface $handlerFactory */ # php-stan hint
Expand All @@ -242,7 +233,7 @@ protected function generateUrl($route, array $parameters = [], int $referenceTyp
{
if ($route instanceof NodesSources) {
/** @var UrlGeneratorInterface $urlGenerator */
$urlGenerator = $this->get('urlGenerator');
$urlGenerator = $this->get(UrlGeneratorInterface::class);
return $urlGenerator->generate(
RouteObjectInterface::OBJECT_BASED_ROUTE_NAME,
array_merge($parameters, [RouteObjectInterface::ROUTE_OBJECT => $route]),
Expand All @@ -253,41 +244,25 @@ protected function generateUrl($route, array $parameters = [], int $referenceTyp
}

/**
* @return string
* @return class-string
*/
public static function getCalledClass()
public static function getCalledClass(): string
{
$className = get_called_class();
if (strpos($className, "\\") !== 0) {
if (!str_starts_with($className, "\\")) {
$className = "\\" . $className;
}
return $className;
}

/**
* Validate a request against a given ROLE_* and throws
* an AccessDeniedException exception.
*
* @param string $role
* @deprecated Use denyAccessUnlessGranted() method instead
* @throws AccessDeniedException
* @return void
*/
public function validateAccessForRole($role)
{
if (!$this->isGranted($role)) {
throw new AccessDeniedException("You don't have access to this page:" . $role);
}
}

/**
* Custom route for redirecting routes with a trailing slash.
*
* @param Request $request
*
* @return RedirectResponse
*/
public function removeTrailingSlashAction(Request $request)
public function removeTrailingSlashAction(Request $request): RedirectResponse
{
$pathInfo = $request->getPathInfo();
$requestUri = $request->getRequestUri();
Expand All @@ -301,7 +276,7 @@ public function removeTrailingSlashAction(Request $request)
* Make translation variable with the good localization.
*
* @param Request $request
* @param string $_locale
* @param string|null $_locale
*
* @return TranslationInterface
* @throws NoTranslationAvailableException
Expand Down Expand Up @@ -394,7 +369,7 @@ protected function getNamespacedView(string $view, string $namespace = ''): stri
* @param int $httpStatus
* @return JsonResponse
*/
public function renderJson(array $data = [], int $httpStatus = JsonResponse::HTTP_OK)
public function renderJson(array $data = [], int $httpStatus = Response::HTTP_OK): JsonResponse
{
return $this->json($data, $httpStatus);
}
Expand Down Expand Up @@ -424,24 +399,25 @@ protected function denyResourceExceptForFormats(Request $request, array $accepta
* @param array $options Options for the form
*
* @return FormBuilderInterface
* @deprecated Use constructor service injection
*/
protected function createNamedFormBuilder(string $name = 'form', $data = null, array $options = [])
{
/** @var FormFactoryInterface $formFactory */
$formFactory = $this->get('form.factory');
$formFactory = $this->get(FormFactoryInterface::class);
return $formFactory->createNamedBuilder($name, FormType::class, $data, $options);
}

/**
* Creates and returns an EntityListManager instance.
*
* @param mixed $entity Entity class path
* @param class-string $entity Entity class path
* @param array $criteria
* @param array $ordering
*
* @return EntityListManagerInterface
*/
public function createEntityListManager($entity, array $criteria = [], array $ordering = [])
public function createEntityListManager(string $entity, array $criteria = [], array $ordering = []): EntityListManagerInterface
{
return new EntityListManager(
$this->getRequest(),
Expand All @@ -457,6 +433,7 @@ public function createEntityListManager($entity, array $criteria = [], array $or
* form by email.
*
* @return ContactFormManager
* @deprecated Use constructor service injection
*/
public function createContactFormManager(): ContactFormManager
{
Expand All @@ -469,6 +446,7 @@ public function createContactFormManager(): ContactFormManager
* Create and return a EmailManager to build and send emails.
*
* @return EmailManager
* @deprecated Use constructor service injection
*/
public function createEmailManager(): EmailManager
{
Expand All @@ -480,26 +458,20 @@ public function createEmailManager(): EmailManager
/**
* Get a user from the tokenStorage.
*
* @return UserInterface|object|null
* @return UserInterface|null
*
* @throws \LogicException If tokenStorage is not available
*
* @see TokenInterface::getUser()
*/
protected function getUser()
protected function getUser(): ?UserInterface
{
if (!$this->has('securityTokenStorage')) {
throw new \LogicException('No TokenStorage has been registered in your application.');
}

/** @var TokenInterface|null $token */
$token = $this->getTokenStorage()->getToken();
if (null === $token) {
return null;
}

$user = $token->getUser();

return \is_object($user) ? $user : null;
return $token?->getUser();
}
}
3 changes: 2 additions & 1 deletion lib/RoadizCompatBundle/src/Controller/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\EntityHandler\NodesSourcesHandler;
use RZ\Roadiz\CoreBundle\ListManager\EntityListManagerInterface;
use RZ\Roadiz\CoreBundle\Routing\NodeRouteHelper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -424,7 +425,7 @@ protected function denyAccessUnlessPublished(): void
/**
* @inheritDoc
*/
public function createEntityListManager($entity, array $criteria = [], array $ordering = [])
public function createEntityListManager(string $entity, array $criteria = [], array $ordering = []): EntityListManagerInterface
{
return parent::createEntityListManager($entity, $criteria, $ordering)
->setAllowRequestSearching(false)
Expand Down
36 changes: 0 additions & 36 deletions lib/RoadizCoreBundle/src/Routing/InstallRouteCollection.php

This file was deleted.

3 changes: 2 additions & 1 deletion lib/RoadizRozierBundle/src/Controller/BackendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RZ\Roadiz\CoreBundle\Bag\NodeTypes;
use RZ\Roadiz\CoreBundle\Bag\Roles;
use RZ\Roadiz\CoreBundle\Bag\Settings;
use RZ\Roadiz\CoreBundle\ListManager\EntityListManagerInterface;
use RZ\Roadiz\CoreBundle\Mailer\EmailManager;
use RZ\Roadiz\Documents\Packages;
use RZ\Roadiz\OpenId\OAuth2LinkGenerator;
Expand Down Expand Up @@ -55,7 +56,7 @@ public static function getSubscribedServices(): array
/**
* @inheritDoc
*/
public function createEntityListManager($entity, array $criteria = [], array $ordering = [])
public function createEntityListManager(string $entity, array $criteria = [], array $ordering = []): EntityListManagerInterface
{
return parent::createEntityListManager($entity, $criteria, $ordering)
->setDisplayingNotPublishedNodes(true);
Expand Down
3 changes: 2 additions & 1 deletion lib/Rozier/src/Forms/CustomFormFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -50,7 +51,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
])
->add(
'defaultValues',
TextType::class,
TextareaType::class,
[
'label' => 'defaultValues',
'required' => false,
Expand Down

0 comments on commit d8d351b

Please sign in to comment.