From d0b4b620dc1b5db574a02b4bce52e8b30e250ae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wojda=C5=82owicz?= Date: Fri, 16 Feb 2024 11:24:35 +0100 Subject: [PATCH] OP-231 - fix phpstan and refactor trait --- composer.json | 4 ++- config/routing/admin_routing.yml | 3 +- phpstan.neon | 15 ++-------- src/ApiClient/SuluApiClient.php | 28 ++++++++++------- .../Action/PurgeSuluCacheAction.php | 30 ++++++++++++++----- src/Controller/Action/RenderPageAction.php | 10 +++++-- src/Entity/ChannelInterface.php | 12 ++++++++ src/Entity/SuluChannelConfigurationTrait.php | 10 +++---- src/Form/Extension/ChannelTypeExtension.php | 2 +- src/Twig/Runtime/SuluRuntime.php | 6 ++-- .../Admin/Channel/Form/_suluConfig.html.twig | 2 +- .../Grid/Action/invalidateSuluCache.html.twig | 2 +- 12 files changed, 77 insertions(+), 47 deletions(-) create mode 100644 src/Entity/ChannelInterface.php diff --git a/composer.json b/composer.json index 59ad9d4..7c83bb2 100644 --- a/composer.json +++ b/composer.json @@ -12,11 +12,13 @@ "php": "^8.0", "sylius/sylius": "^1.12", "sylius/mailer-bundle": "^1.8 || ^2.0@beta", - "symfony/webpack-encore-bundle": "^1.15" + "symfony/webpack-encore-bundle": "^1.15", + "symfony/http-client": "^5.4 || ^6.0" }, "require-dev": { "behat/behat": "^3.6.1", "behat/mink-selenium2-driver": "^1.4", + "bitbag/coding-standard": "^3.0", "dmore/behat-chrome-extension": "^1.3", "dmore/chrome-mink-driver": "^2.7", "friends-of-behat/mink": "^1.8", diff --git a/config/routing/admin_routing.yml b/config/routing/admin_routing.yml index 602328e..872ace6 100644 --- a/config/routing/admin_routing.yml +++ b/config/routing/admin_routing.yml @@ -1,7 +1,8 @@ bitbag_purge_sulu_cache_for_channel: - path: /invalidate-sulu-cache/{locale}/{id} + path: /invalidate-sulu-cache/{id}/{locale} methods: [GET] defaults: + locale: '' _controller: bitbag.sylius_sulu_plugin.controller.action.purge_sulu_cache_action _sylius: section: admin diff --git a/phpstan.neon b/phpstan.neon index 2235744..3f831ad 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,18 +1,7 @@ +includes: + - vendor/bitbag/coding-standard/phpstan.neon parameters: level: max - reportUnmatchedIgnoredErrors: false - checkMissingIterableValueType: false - paths: - - src - - tests/Behat - - excludes_analyse: - # Makes PHPStan crash - - 'src/DependencyInjection/Configuration.php' - - # Test dependencies - - 'tests/Application/app/**.php' - - 'tests/Application/src/**.php' ignoreErrors: - '/Parameter #1 \$configuration of method Symfony\\Component\\DependencyInjection\\Extension\\Extension::processConfiguration\(\) expects Symfony\\Component\\Config\\Definition\\ConfigurationInterface, Symfony\\Component\\Config\\Definition\\ConfigurationInterface\|null given\./' diff --git a/src/ApiClient/SuluApiClient.php b/src/ApiClient/SuluApiClient.php index da37606..d98694d 100644 --- a/src/ApiClient/SuluApiClient.php +++ b/src/ApiClient/SuluApiClient.php @@ -10,6 +10,7 @@ use Symfony\Component\HttpKernel\HttpCache\Store; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; +use Webmozart\Assert\Assert; final class SuluApiClient { @@ -24,18 +25,21 @@ public function __construct( public function fetchCmsContent(string $url): array { $channel = $this->shopperContext->getChannel(); + $locale = ''; + $storeRoot = sprintf( + '%s/%s', + $this->cacheDir, + $channel->getCode() ?? 'GLOBAL', + ); + + Assert::methodExists($channel, 'isSuluUseLocalizedUrls'); - $locale = null; - if ($channel->isSuluUseLocalizationUrl()) { + if ($channel->isSuluUseLocalizedUrls()) { $locale = $this->shopperContext->getLocaleCode(); + $storeRoot = sprintf('%s/%s', $storeRoot, $locale); } - $store = new Store(sprintf( - '%s/%s/%s', - $this->cacheDir, - $channel->getCode() ?? 'GLOBAL', - $locale, - )); + $store = new Store($storeRoot); $this->client = new CachingHttpClient($this->client, $store); @@ -47,7 +51,11 @@ public function fetchCmsContent(string $url): array return []; } - return json_decode($response->getContent(), true); + $response = json_decode($response->getContent(), true); + + Assert::isArray($response); + + return $response; } private function makeApiCall(string $url): ResponseInterface @@ -65,7 +73,7 @@ private function makeApiCall(string $url): ResponseInterface private function createUrl(string $url, ?string $locale = null): string { - if (null === $locale) { + if (null === $locale || strlen($locale) === 0) { return sprintf('%s/%s.json', $this->suluBaseUri, $url); } diff --git a/src/Controller/Action/PurgeSuluCacheAction.php b/src/Controller/Action/PurgeSuluCacheAction.php index 9442c4d..e090a98 100644 --- a/src/Controller/Action/PurgeSuluCacheAction.php +++ b/src/Controller/Action/PurgeSuluCacheAction.php @@ -5,11 +5,14 @@ namespace BitBag\SyliusSuluPlugin\Controller\Action; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; +use Sylius\Component\Core\Model\ChannelInterface; use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Response; use Symfony\Contracts\Translation\TranslatorInterface; +use Webmozart\Assert\Assert; final class PurgeSuluCacheAction { @@ -21,18 +24,24 @@ public function __construct( ) { } - public function __invoke(Request $request) + public function __invoke(Request $request): Response { $channelId = $request->get('id'); $localeCode = $request->get('locale'); + /** @var ChannelInterface $channel */ $channel = $this->channelRepository->find($channelId); + $cacheDir = sprintf('%s/%s', $this->cacheDir, $channel->getCode() ?? 'GLOBAL'); - if (null !== $localeCode) { - $cacheDir = sprintf('%s/%s/%s', $this->cacheDir, $localeCode, $channel->getCode() ?? 'GLOBAL'); + if (is_string($localeCode) && strlen($localeCode) !== 0) { + $cacheDir = sprintf('%s/%s/%s', $this->cacheDir, $channel->getCode() ?? 'GLOBAL', $localeCode); } - $response = new RedirectResponse($request->server->get('HTTP_REFERER'), 302); + $referer = $request->server->get('HTTP_REFERER'); + + Assert::string($referer); + + $response = new RedirectResponse($referer, 302); if (!is_dir($cacheDir)) { $this->addFlash('error', 'bitbag.sulu_plugin.cache.dir_not_exists'); @@ -48,7 +57,7 @@ public function __invoke(Request $request) return $response; } - $this->addFlash('success', 'bitbag.sulu_plugin.cache.successful_purged', ); + $this->addFlash('success', 'bitbag.sulu_plugin.cache.successful_purged'); return $response; } @@ -58,6 +67,7 @@ private function removeDir(string $dir): void $iterator = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS); $files = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST); + /** @var \SplFileInfo $file */ foreach ($files as $file) { if ($file->isDir()) { rmdir($file->getPathname()); @@ -71,12 +81,16 @@ private function removeDir(string $dir): void private function addFlash(string $type, string $translation): void { - $flashbag = $this->requestStack->getSession()->getFlashBag(); + $session = $this->requestStack->getSession(); + + Assert::methodExists($session, 'getFlashBag'); + + $flashbag = $session->getFlashBag(); $flashbag->add( - 'success', + $type, $this->translator->trans( - 'bitbag.sulu_plugin.cache.successful_purged', + $translation, domain: 'flashes', ), ); diff --git a/src/Controller/Action/RenderPageAction.php b/src/Controller/Action/RenderPageAction.php index 59d3eba..89aed8c 100644 --- a/src/Controller/Action/RenderPageAction.php +++ b/src/Controller/Action/RenderPageAction.php @@ -8,6 +8,7 @@ use BitBag\SyliusSuluPlugin\Renderer\Page\SuluPageRendererStrategy; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Webmozart\Assert\Assert; final class RenderPageAction { @@ -20,15 +21,18 @@ public function __construct( public function __invoke(Request $request): Response { $url = $request->get('slug'); + $secondSlug = $request->get('second_slug'); - if (null !== $request->get('second_slug')) { - $url = sprintf('%s/%s', $url, $request->get('second_slug')); + Assert::string($url); + + if (is_string($secondSlug) && strlen($secondSlug) !== 0) { + $url = sprintf('%s/%s', $url, $secondSlug); } $page = $this->suluApiClient->fetchCmsContent($url); $page = $this->pageRendererStrategy->renderPage($page); - if (null === $page) { + if (strlen($page) === 0) { return new Response(null, 404); } diff --git a/src/Entity/ChannelInterface.php b/src/Entity/ChannelInterface.php new file mode 100644 index 0000000..7a5e04f --- /dev/null +++ b/src/Entity/ChannelInterface.php @@ -0,0 +1,12 @@ +suluUseLocalizationUrl; + return $this->suluUseLocalizedUrls; } - public function setSuluUseLocalizationUrl(bool $suluUseLocalizationUrl): void + public function setSuluUseLocalizedUrls(bool $suluUseLocalizedUrls): void { - $this->suluUseLocalizationUrl = $suluUseLocalizationUrl; + $this->suluUseLocalizedUrls = $suluUseLocalizedUrls; } } diff --git a/src/Form/Extension/ChannelTypeExtension.php b/src/Form/Extension/ChannelTypeExtension.php index 22f5b32..fe84f6a 100644 --- a/src/Form/Extension/ChannelTypeExtension.php +++ b/src/Form/Extension/ChannelTypeExtension.php @@ -14,7 +14,7 @@ final class ChannelTypeExtension extends AbstractTypeExtension public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('suluUseLocalizationUrl', CheckboxType::class, [ + ->add('suluUseLocalizedUrls', CheckboxType::class, [ 'label' => 'bitbag.sulu_plugin.use_localized_url', ]) ; diff --git a/src/Twig/Runtime/SuluRuntime.php b/src/Twig/Runtime/SuluRuntime.php index 9ba3078..7451ba9 100644 --- a/src/Twig/Runtime/SuluRuntime.php +++ b/src/Twig/Runtime/SuluRuntime.php @@ -33,7 +33,7 @@ public function renderSuluPage(string $id): string public function renderSuluBlocks(array $blocks, ?string $divider = null): string { - $content = null; + $content = ''; foreach ($blocks as $block) { $content .= $this->blockRendererStrategy->renderBlock($block); @@ -46,7 +46,7 @@ public function renderSuluBlocks(array $blocks, ?string $divider = null): string public function renderSuluBlocksWithType(array $blocks, string $type, ?string $divider = null): string { $blocks = array_filter($blocks, fn (array $block) => $block['type'] === $type); - $content = null; + $content = ''; foreach ($blocks as $block) { $content .= $this->blockRendererStrategy->renderBlock($block); @@ -63,7 +63,7 @@ public function renderSuluBlockWithType(array $blocks, string $type): string if (count($blocks) > 1) { $blocks = $blocks[0]; } - $content = null; + $content = ''; foreach ($blocks as $block) { $content .= $this->blockRendererStrategy->renderBlock($block); diff --git a/templates/Admin/Channel/Form/_suluConfig.html.twig b/templates/Admin/Channel/Form/_suluConfig.html.twig index 8ead2bc..7cb333e 100644 --- a/templates/Admin/Channel/Form/_suluConfig.html.twig +++ b/templates/Admin/Channel/Form/_suluConfig.html.twig @@ -1,5 +1,5 @@

{{ 'bitbag.sulu_plugin.sulu_config'|trans }}

- {{ form_row(form.suluUseLocalizationUrl) }} + {{ form_row(form.suluUseLocalizedUrls) }}
diff --git a/templates/Admin/Grid/Action/invalidateSuluCache.html.twig b/templates/Admin/Grid/Action/invalidateSuluCache.html.twig index 90f5c08..a4050b8 100644 --- a/templates/Admin/Grid/Action/invalidateSuluCache.html.twig +++ b/templates/Admin/Grid/Action/invalidateSuluCache.html.twig @@ -2,7 +2,7 @@ {% set baseParameters = options.link.parameters %} -{% if true == data.isSuluUseLocalizationUrl %} +{% if true == data.isSuluUseLocalizedUrls %} {% for locale in data.locales %} {% set parameters = baseParameters|merge({'locale': locale.code})%} {% set path = options.link.url|default(path(options.link.route, parameters)) %}