From 1495ab5bd1cc70749cd2aeae44e2bafee23c7f27 Mon Sep 17 00:00:00 2001 From: Etienne Gutbub Date: Tue, 5 Sep 2023 17:46:59 +0200 Subject: [PATCH] fix: phpcs --- .php-cs-fixer.dist.php | 2 +- src/Form/Extension/ChannelTypeExtension.php | 3 +- src/Form/Type/Settings/NoCommerceType.php | 2 - src/Kernel/SyliusNoCommerceKernelTrait.php | 7 +- src/Menu/AdminCustomerShowMenuListener.php | 3 +- src/Menu/AdminMenuListener.php | 3 +- src/Menu/ShopAccountMenuListener.php | 3 +- src/Provider/FeaturesProvider.php | 32 ++-- src/Provider/FeaturesProviderInterface.php | 8 +- .../TemplateBlockRegistryDecorator.php | 145 ++++++++++++++++++ src/Resources/config/config.yaml | 1 - src/Resources/config/services.yaml | 6 + src/Resources/config/sylius/ui.yaml | 56 ------- 13 files changed, 183 insertions(+), 88 deletions(-) create mode 100644 src/Registry/TemplateBlockRegistryDecorator.php delete mode 100644 src/Resources/config/sylius/ui.yaml diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 75a6bef..2a5c63b 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -49,6 +49,7 @@ 'binary_operator_spaces' => true, 'blank_line_after_opening_tag' => true, 'blank_line_after_namespace' => true, + 'blank_lines_before_namespace' => true, 'blank_line_before_statement' => true, 'braces' => [ 'allow_single_line_closure' => true, @@ -235,7 +236,6 @@ 'self_accessor' => true, 'short_scalar_cast' => true, 'single_blank_line_at_eof' => true, - 'single_blank_line_before_namespace' => true, 'single_class_element_per_statement' => true, 'single_import_per_statement' => true, 'single_line_after_imports' => true, diff --git a/src/Form/Extension/ChannelTypeExtension.php b/src/Form/Extension/ChannelTypeExtension.php index 1081c71..5825338 100644 --- a/src/Form/Extension/ChannelTypeExtension.php +++ b/src/Form/Extension/ChannelTypeExtension.php @@ -28,8 +28,7 @@ final class ChannelTypeExtension extends AbstractTypeExtension public function __construct( FeaturesProviderInterface $featuresProvider - ) - { + ) { $this->featuresProvider = $featuresProvider; } diff --git a/src/Form/Type/Settings/NoCommerceType.php b/src/Form/Type/Settings/NoCommerceType.php index e5c5d48..42106e0 100644 --- a/src/Form/Type/Settings/NoCommerceType.php +++ b/src/Form/Type/Settings/NoCommerceType.php @@ -18,8 +18,6 @@ use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\FormInterface; -use Symfony\Component\Form\FormView; class NoCommerceType extends AbstractSettingsType { diff --git a/src/Kernel/SyliusNoCommerceKernelTrait.php b/src/Kernel/SyliusNoCommerceKernelTrait.php index 5ee950a..077c845 100644 --- a/src/Kernel/SyliusNoCommerceKernelTrait.php +++ b/src/Kernel/SyliusNoCommerceKernelTrait.php @@ -208,6 +208,9 @@ trait SyliusNoCommerceKernelTrait ], ]; + /** + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + */ public function loadRoutes(LoaderInterface $loader): RouteCollection { $collection = $this->parentLoadRoutes($loader); @@ -220,7 +223,7 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection $routesToRemove = $this->getRoutesToRemove(); foreach ($collection as $name => $route) { foreach ($routesToRemove as $routeToRemove) { - if (false !== strpos($name, $routeToRemove)) { + if (str_contains($name, $routeToRemove)) { $route->setCondition('1 == 0'); } } @@ -229,7 +232,7 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection return $collection; } - private function setFeatureProvider(FeaturesProviderInterface $featuresProvider) + private function setFeatureProvider(FeaturesProviderInterface $featuresProvider): void { $this->featuresProvider = $featuresProvider; } diff --git a/src/Menu/AdminCustomerShowMenuListener.php b/src/Menu/AdminCustomerShowMenuListener.php index 2e8d6cb..df51d26 100644 --- a/src/Menu/AdminCustomerShowMenuListener.php +++ b/src/Menu/AdminCustomerShowMenuListener.php @@ -22,8 +22,7 @@ final class AdminCustomerShowMenuListener public function __construct( FeaturesProviderInterface $featuresProvider - ) - { + ) { $this->featuresProvider = $featuresProvider; } diff --git a/src/Menu/AdminMenuListener.php b/src/Menu/AdminMenuListener.php index a649f8a..46b29e4 100644 --- a/src/Menu/AdminMenuListener.php +++ b/src/Menu/AdminMenuListener.php @@ -27,8 +27,7 @@ final class AdminMenuListener public function __construct( ConfigInterface $config, FeaturesProviderInterface $featuresProvider - ) - { + ) { $this->config = $config; $this->featuresProvider = $featuresProvider; } diff --git a/src/Menu/ShopAccountMenuListener.php b/src/Menu/ShopAccountMenuListener.php index 08cba11..246632f 100644 --- a/src/Menu/ShopAccountMenuListener.php +++ b/src/Menu/ShopAccountMenuListener.php @@ -22,8 +22,7 @@ final class ShopAccountMenuListener public function __construct( FeaturesProviderInterface $featuresProvider - ) - { + ) { $this->featuresProvider = $featuresProvider; } diff --git a/src/Provider/FeaturesProvider.php b/src/Provider/FeaturesProvider.php index 78b31c0..5e2676c 100644 --- a/src/Provider/FeaturesProvider.php +++ b/src/Provider/FeaturesProvider.php @@ -1,11 +1,11 @@ + * (c) Monsieur Biz * - * For the full copyright and license information, please view the LICENSE + * For the full copyright and license information, please view the LICENSE.txt * file that was distributed with this source code. */ @@ -13,6 +13,7 @@ namespace MonsieurBiz\SyliusNoCommercePlugin\Provider; +use Exception; use MonsieurBiz\SyliusSettingsPlugin\Settings\SettingsInterface; use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Core\Model\ChannelInterface; @@ -22,26 +23,29 @@ final class FeaturesProvider implements FeaturesProviderInterface private ChannelContextInterface $channelContext; private SettingsInterface $nocommerceSettings; + public function __construct( ChannelContextInterface $channelContext, SettingsInterface $nocommerceSettings - ) - { + ) { $this->channelContext = $channelContext; $this->nocommerceSettings = $nocommerceSettings; } - public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null): bool + public function isNoCommerceEnabledForChannel(ChannelInterface $channel = null): bool { - if (null === $channel) { - $channel = $this->channelContext->getChannel(); - } - // In case we are getting a channel that does not exists yet we pass null as the channel to retrieve the setting value of the global scope - if (null === $channel->getId()) { - $channel = null; + try { + if (null === $channel) { + $channel = $this->channelContext->getChannel(); + } + // In case we are getting a channel that does not exists yet we pass null as the channel to retrieve the setting value of the global scope + if (null === $channel->getId()) { + $channel = null; + } + } catch (Exception $exception) { + return false; } - return $this->nocommerceSettings->getCurrentValue($channel, null, 'enabled'); + return (bool) $this->nocommerceSettings->getCurrentValue($channel, null, 'enabled'); } - } diff --git a/src/Provider/FeaturesProviderInterface.php b/src/Provider/FeaturesProviderInterface.php index 171cf06..4ecae2a 100644 --- a/src/Provider/FeaturesProviderInterface.php +++ b/src/Provider/FeaturesProviderInterface.php @@ -1,11 +1,11 @@ + * (c) Monsieur Biz * - * For the full copyright and license information, please view the LICENSE + * For the full copyright and license information, please view the LICENSE.txt * file that was distributed with this source code. */ @@ -17,5 +17,5 @@ interface FeaturesProviderInterface { - public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null): bool; + public function isNoCommerceEnabledForChannel(ChannelInterface $channel = null): bool; } diff --git a/src/Registry/TemplateBlockRegistryDecorator.php b/src/Registry/TemplateBlockRegistryDecorator.php new file mode 100644 index 0000000..e061e9e --- /dev/null +++ b/src/Registry/TemplateBlockRegistryDecorator.php @@ -0,0 +1,145 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusNoCommercePlugin\Registry; + +use Laminas\Stdlib\SplPriorityQueue; +use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProviderInterface; +use Sylius\Bundle\UiBundle\Registry\TemplateBlock; +use Sylius\Bundle\UiBundle\Registry\TemplateBlockRegistryInterface; + +final class TemplateBlockRegistryDecorator implements TemplateBlockRegistryInterface +{ + private TemplateBlockRegistryInterface $templateBlockRegistry; + + private FeaturesProviderInterface $featuresProvider; + + /** @var array|array[] */ + private array $disableEvents = [ + 'sylius.admin.dashboard.content' => [ + 'before_header_legacy', + 'after_header_legacy', + 'statistics', + 'after_statistics_legacy', + 'menu', + 'after_menu_legacy', + 'latest', + 'after_content_legacy', + ], + 'sylius.shop.layout.header.grid' => [ + 'cart', + ], + 'sylius.shop.layout.header' => [ + 'menu', + ], + 'sylius.shop.homepage' => [ + 'latest_products', + 'latest_products_carousel', + 'products_grid', + ], + 'sylius.admin.customer.show.content' => [ + 'statistics', + ], + 'sylius.admin.customer.show.address' => [ + 'header', + 'content', + ], + 'sylius.admin.layout.topbar_left' => [ + 'search', + ], + ]; + + public function __construct( + TemplateBlockRegistryInterface $templateBlockRegistry, + FeaturesProviderInterface $featuresProvider + ) { + $this->templateBlockRegistry = $templateBlockRegistry; + $this->featuresProvider = $featuresProvider; + } + + public function findEnabledForEvents(array $eventNames): array + { + // No need to sort blocks again if there's only one event because we have them sorted already + if (1 === \count($eventNames)) { + $eventName = reset($eventNames); + + $arrayBlocks = $this->all()[$eventName] ?? []; + $arrayBlocks = $this->removeNoCommerceBlocks($eventName, $arrayBlocks); + + return array_values(array_filter( + $arrayBlocks, + static fn (TemplateBlock $templateBlock): bool => $templateBlock->isEnabled(), + )); + } + + $enabledFinalizedTemplateBlocks = array_filter( + $this->findFinalizedForEvents($eventNames), + static fn (TemplateBlock $templateBlock): bool => $templateBlock->isEnabled(), + ); + + $templateBlocksPriorityQueue = new SplPriorityQueue(); + foreach ($enabledFinalizedTemplateBlocks as $templateBlock) { + $templateBlocksPriorityQueue->insert($templateBlock, $templateBlock->getPriority()); + } + + /** @phpstan-ignore-next-line */ + return $templateBlocksPriorityQueue->toArray(); + } + + public function all(): array + { + return $this->templateBlockRegistry->all(); + } + + private function findFinalizedForEvents(array $eventNames): array + { + /** + * @var TemplateBlock[] $finalizedTemplateBlocks + * + * @psalm-var array $finalizedTemplateBlocks + */ + $finalizedTemplateBlocks = []; + $reversedEventNames = array_reverse($eventNames); + + foreach ($reversedEventNames as $eventName) { + $templateBlocks = $this->all()[$eventName] ?? []; + $templateBlocks = $this->removeNoCommerceBlocks($eventName, $templateBlocks); + foreach ($templateBlocks as $blockName => $templateBlock) { + if (\array_key_exists($blockName, $finalizedTemplateBlocks)) { + $templateBlock = $finalizedTemplateBlocks[$blockName]->overwriteWith($templateBlock); + } + + $finalizedTemplateBlocks[$blockName] = $templateBlock; + } + } + + return $finalizedTemplateBlocks; + } + + /** + * @return TemplateBlock[] + */ + private function removeNoCommerceBlocks(string $eventName, array $arrayBlocks): array + { + // Remove block from no Commerce + if ($this->featuresProvider->isNoCommerceEnabledForChannel()) { + if (isset($this->disableEvents[$eventName])) { + foreach ($this->disableEvents[$eventName] as $block) { + unset($arrayBlocks[$block]); + } + } + } + + return $arrayBlocks; + } +} diff --git a/src/Resources/config/config.yaml b/src/Resources/config/config.yaml index dc901a0..30d37d6 100644 --- a/src/Resources/config/config.yaml +++ b/src/Resources/config/config.yaml @@ -1,4 +1,3 @@ imports: - - { resource: "sylius/ui.yaml" } - { resource: "sylius/grid.yaml" } - { resource: "monsieurbiz/settings.yaml" } diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 841e310..297ff31 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -51,6 +51,12 @@ services: MonsieurBiz\SyliusNoCommercePlugin\Context\NoCurrencyContext: decorates: sylius.context.currency.channel_aware + + MonsieurBiz\SyliusNoCommercePlugin\Registry\TemplateBlockRegistryDecorator: + decorates: 'Sylius\Bundle\UiBundle\Registry\TemplateBlockRegistryInterface' + arguments: + $templateBlockRegistry: '@.inner' + $featuresProvider: '@monsieurbiz.no_commerce.provider.features_provider' monsieurbiz.no_commerce.provider.features_provider: class: 'MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProvider' diff --git a/src/Resources/config/sylius/ui.yaml b/src/Resources/config/sylius/ui.yaml deleted file mode 100644 index 056369d..0000000 --- a/src/Resources/config/sylius/ui.yaml +++ /dev/null @@ -1,56 +0,0 @@ -sylius_ui: - events: - sylius.admin.dashboard.content: - blocks: - before_header_legacy: - enabled: false - after_header_legacy: - enabled: false - statistics: - enabled: false - after_statistics_legacy: - enabled: false - menu: - enabled: false - after_menu_legacy: - enabled: false - latest: - enabled: false - after_content_legacy: - enabled: false - - sylius.shop.layout.header.grid: - blocks: - cart: - enabled: false - - sylius.shop.layout.header: - blocks: - menu: - enabled: false - - sylius.shop.homepage: - blocks: - latest_products: - enabled: false - latest_products_carousel: - enabled: false - products_grid: - enabled: false - - sylius.admin.customer.show.content: - blocks: - statistics: - enabled: false - - sylius.admin.customer.show.address: - blocks: - header: - enabled: false - content: - enabled: false - - sylius.admin.layout.topbar_left: - blocks: - search: - enabled: false