Skip to content

Commit

Permalink
OP-231 - fix phpstan and refactor trait
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszWojdalowicz committed Feb 16, 2024
1 parent 6f103de commit d0b4b62
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 47 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion config/routing/admin_routing.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 2 additions & 13 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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\./'
28 changes: 18 additions & 10 deletions src/ApiClient/SuluApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -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);
}

Expand Down
30 changes: 22 additions & 8 deletions src/Controller/Action/PurgeSuluCacheAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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');
Expand All @@ -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;
}
Expand All @@ -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());
Expand All @@ -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',
),
);
Expand Down
10 changes: 7 additions & 3 deletions src/Controller/Action/RenderPageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Entity/ChannelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace BitBag\SyliusSuluPlugin\Entity;

interface ChannelInterface
{
public function isSuluUseLocalizedUrls(): bool;

public function setSuluUseLocalizedUrls(bool $suluUseLocalizedUrls): void;
}
10 changes: 5 additions & 5 deletions src/Entity/SuluChannelConfigurationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

trait SuluChannelConfigurationTrait
{
protected bool $suluUseLocalizationUrl = false;
protected bool $suluUseLocalizedUrls = false;

public function isSuluUseLocalizationUrl(): bool
public function isSuluUseLocalizedUrls(): bool
{
return $this->suluUseLocalizationUrl;
return $this->suluUseLocalizedUrls;
}

public function setSuluUseLocalizationUrl(bool $suluUseLocalizationUrl): void
public function setSuluUseLocalizedUrls(bool $suluUseLocalizedUrls): void
{
$this->suluUseLocalizationUrl = $suluUseLocalizationUrl;
$this->suluUseLocalizedUrls = $suluUseLocalizedUrls;
}
}
2 changes: 1 addition & 1 deletion src/Form/Extension/ChannelTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
])
;
Expand Down
6 changes: 3 additions & 3 deletions src/Twig/Runtime/SuluRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion templates/Admin/Channel/Form/_suluConfig.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="ui hidden divider"></div>
<h4 class="ui top attached large header">{{ 'bitbag.sulu_plugin.sulu_config'|trans }}</h4>
<div class="ui attached segment">
{{ form_row(form.suluUseLocalizationUrl) }}
{{ form_row(form.suluUseLocalizedUrls) }}
</div>
2 changes: 1 addition & 1 deletion templates/Admin/Grid/Action/invalidateSuluCache.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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)) %}
Expand Down

0 comments on commit d0b4b62

Please sign in to comment.