Skip to content

Commit

Permalink
fix: phpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-monsieurbiz committed Sep 5, 2023
1 parent 8f71e30 commit 1495ab5
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/Form/Extension/ChannelTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ final class ChannelTypeExtension extends AbstractTypeExtension

public function __construct(
FeaturesProviderInterface $featuresProvider
)
{
) {
$this->featuresProvider = $featuresProvider;
}

Expand Down
2 changes: 0 additions & 2 deletions src/Form/Type/Settings/NoCommerceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
7 changes: 5 additions & 2 deletions src/Kernel/SyliusNoCommerceKernelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ trait SyliusNoCommerceKernelTrait
],
];

/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function loadRoutes(LoaderInterface $loader): RouteCollection
{
$collection = $this->parentLoadRoutes($loader);
Expand All @@ -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');
}
}
Expand All @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Menu/AdminCustomerShowMenuListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ final class AdminCustomerShowMenuListener

public function __construct(
FeaturesProviderInterface $featuresProvider
)
{
) {
$this->featuresProvider = $featuresProvider;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Menu/AdminMenuListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ final class AdminMenuListener
public function __construct(
ConfigInterface $config,
FeaturesProviderInterface $featuresProvider
)
{
) {
$this->config = $config;
$this->featuresProvider = $featuresProvider;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Menu/ShopAccountMenuListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ final class ShopAccountMenuListener

public function __construct(
FeaturesProviderInterface $featuresProvider
)
{
) {
$this->featuresProvider = $featuresProvider;
}

Expand Down
32 changes: 18 additions & 14 deletions src/Provider/FeaturesProvider.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php

/*
* This file is part of SyliusNoCommercePlugin corporate website.
* This file is part of Monsieur Biz' No Commerce plugin for Sylius.
*
* (c) SyliusNoCommercePlugin <sylius+syliusnocommerceplugin@monsieurbiz.com>
* (c) Monsieur Biz <[email protected]>
*
* 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.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusNoCommercePlugin\Provider;

use Exception;
use MonsieurBiz\SyliusSettingsPlugin\Settings\SettingsInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
Expand All @@ -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');
}

}
8 changes: 4 additions & 4 deletions src/Provider/FeaturesProviderInterface.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/*
* This file is part of SyliusNoCommercePlugin corporate website.
* This file is part of Monsieur Biz' No Commerce plugin for Sylius.
*
* (c) SyliusNoCommercePlugin <sylius+syliusnocommerceplugin@monsieurbiz.com>
* (c) Monsieur Biz <[email protected]>
*
* 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.
*/

Expand All @@ -17,5 +17,5 @@

interface FeaturesProviderInterface
{
public function isNoCommerceEnabledForChannel(?ChannelInterface $channel = null): bool;
public function isNoCommerceEnabledForChannel(ChannelInterface $channel = null): bool;
}
145 changes: 145 additions & 0 deletions src/Registry/TemplateBlockRegistryDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

/*
* This file is part of Monsieur Biz' No Commerce plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* 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<string, TemplateBlock> $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;
}
}
1 change: 0 additions & 1 deletion src/Resources/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
imports:
- { resource: "sylius/ui.yaml" }
- { resource: "sylius/grid.yaml" }
- { resource: "monsieurbiz/settings.yaml" }
6 changes: 6 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
56 changes: 0 additions & 56 deletions src/Resources/config/sylius/ui.yaml

This file was deleted.

0 comments on commit 1495ab5

Please sign in to comment.