Skip to content

Commit

Permalink
Merge pull request #30 from etienne-monsieurbiz/feature/settings-per-…
Browse files Browse the repository at this point in the history
…canal

Setting per channel
  • Loading branch information
maximehuran authored Sep 6, 2023
2 parents 8f10309 + fb9aa48 commit b5a08c2
Show file tree
Hide file tree
Showing 27 changed files with 643 additions and 249 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ jobs:
fail-fast: false
matrix:
php: ['7.4', '8.0', '8.1']
sylius: ['~1.8.0', '~1.9.0', '~1.10.0', '~1.11.0', '~1.12.0']
sylius: ['~1.9.0', '~1.10.0', '~1.11.0', '~1.12.0']
exclude:
- php: '8.0'
sylius: '~1.8.0'
- php: '8.0'
sylius: '~1.9.0'
- php: '8.1'
sylius: '~1.8.0'
- php: '8.1'
sylius: '~1.9.0'
- php: '7.4'
Expand Down
3 changes: 2 additions & 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 @@ -115,6 +116,7 @@
'lowercase_static_reference' => true,
'magic_constant_casing' => true,
'method_argument_space' => true,
'modernize_strpos' => false,
'modernize_types_casting' => true,
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => [
Expand Down Expand Up @@ -235,7 +237,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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ test.twig: ## Validate Twig templates
### SYLIUS
### ¯¯¯¯¯¯

sylius: dependencies sylius.database sylius.fixtures sylius.assets ## Install Sylius
sylius: dependencies sylius.database sylius.fixtures sylius.assets messenger.setup ## Install Sylius
.PHONY: sylius

sylius.database: ## Setup the database
Expand All @@ -166,6 +166,9 @@ sylius.assets: ## Install all assets with symlinks
${CONSOLE} sylius:install:assets
${CONSOLE} sylius:theme:assets:install --symlink

messenger.setup: ## Setup Messenger transports
${CONSOLE} messenger:setup-transports

###
### PLATFORM
### ¯¯¯¯¯¯¯¯
Expand Down
89 changes: 5 additions & 84 deletions dist/templates/bundles/SyliusAdminBundle/Channel/_form.html.twig
Original file line number Diff line number Diff line change
@@ -1,84 +1,5 @@
{{ form_errors(form) }}
<div class="ui two column stackable grid">
<div class="column">
<h4 class="ui top attached large header">{{ 'sylius.ui.general'|trans }}</h4>
<div class="ui attached segment">
{{ form_errors(form) }}
<div class="fields">
<div class="six wide field">
{{ form_row(form.code) }}
</div>
<div class="seven wide field">
{{ form_row(form.name) }}
</div>
<div class="three wide field">
{{ form_row(form.color) }}
</div>
</div>
<div class="ui hidden divider"></div>
{{ form_row(form.enabled) }}
</div>
<div class="ui attached segment">
<div class="field">
{{ form_label(form.hostname) }}
<div class="ui labeled input">
<div class="ui label">https://</div>
{{ form_widget(form.hostname) }}
</div>
{{ form_errors(form.hostname) }}
</div>
{{ form_row(form.contactEmail) }}
{{ form_row(form.description, {'attr': {'rows' : '3'}}) }}
</div>
<div class="ui attached segment">
{{ form_row(form.countries) }}
</div>
{# <div class="ui hidden divider"></div>#}
{# <h4 class="ui top attached large header">{{ 'sylius.ui.money'|trans }}</h4>#}
{# <div class="ui attached segment">#}
{# <div class="two fields">#}
{# {{ form_row(form.baseCurrency) }}#}
{# {{ form_row(form.currencies) }}#}
{# </div>#}
{# </div>#}
{# <div class="ui attached segment">#}
{# {{ form_row(form.defaultTaxZone) }}#}
{# {{ form_row(form.taxCalculationStrategy) }}#}
{# </div>#}
</div>
<div class="column">
{# <h4 class="ui top attached large header">{{ form_label(form.shopBillingData) }}</h4>#}
{# <div class="ui attached segment">#}
{# <div class="two fields">#}
{# {{ form_row(form.shopBillingData.company) }}#}
{# {{ form_row(form.shopBillingData.taxId) }}#}
{# </div>#}
{# <div class="two fields">#}
{# {{ form_row(form.shopBillingData.countryCode) }}#}
{# {{ form_row(form.shopBillingData.street) }}#}
{# </div>#}
{# <div class="two fields">#}
{# {{ form_row(form.shopBillingData.city) }}#}
{# {{ form_row(form.shopBillingData.postcode) }}#}
{# </div>#}
{# </div>#}
{# <div class="ui hidden divider"></div>#}
<h4 class="ui top attached large header">{{ 'sylius.ui.look_and_feel'|trans }}</h4>
<div class="ui attached segment">
{{ form_row(form.themeName) }}
</div>
<div class="ui attached segment">
{{ form_row(form.locales) }}
{{ form_row(form.defaultLocale) }}
</div>
{# <div class="ui attached segment">#}
{# {{ form_row(form.menuTaxon) }}#}
{# </div>#}
<div class="ui hidden divider"></div>
<div class="ui attached segment">
{# {{ form_row(form.skippingShippingStepAllowed) }}#}
{# {{ form_row(form.skippingPaymentStepAllowed) }}#}
{{ form_row(form.accountVerificationRequired) }}
</div>
</div>
</div>
{% if form.vars.noCommerceEnabled %}
{% include '@MonsieurBizSyliusNoCommercePlugin/Admin/Channel/_noCommerceForm.html.twig' %}
{% else %}
{% include '@MonsieurBizSyliusNoCommercePlugin/Admin/Channel/_standardForm.html.twig' %}
{% endif %}
54 changes: 43 additions & 11 deletions src/Collector/NoCommerceSyliusCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
namespace MonsieurBiz\SyliusNoCommercePlugin\Collector;

use MonsieurBiz\SyliusNoCommercePlugin\Context\NoCurrencyContext;
use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProviderInterface;
use Sylius\Bundle\CoreBundle\Application\Kernel;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Core\Context\ShopperContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Currency\Context\CurrencyNotFoundException;
use Sylius\Component\Locale\Context\LocaleNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -26,24 +30,39 @@ final class NoCommerceSyliusCollector extends DataCollector
{
private ShopperContextInterface $shopperContext;

private FeaturesProviderInterface $featuresProvider;

/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function __construct(
ShopperContextInterface $shopperContext,
array $bundles,
string $defaultLocaleCode
string $defaultLocaleCode,
FeaturesProviderInterface $featuresProvider
) {
$this->shopperContext = $shopperContext;
$this->featuresProvider = $featuresProvider;
$this->data = [
'version' => Kernel::VERSION,
'base_currency_code' => NoCurrencyContext::NONE_CURRENCY_CODE,
'currency_code' => NoCurrencyContext::NONE_CURRENCY_CODE,
'version' => Kernel::VERSION . ($this->featuresProvider->isNoCommerceEnabledForChannel() ? ' NoCommerce' : ''),
'base_currency_code' => null,
'currency_code' => null,
'default_locale_code' => $defaultLocaleCode,
'locale_code' => null,
'extensions' => [
'MonsieurBizNoCommercePlugin' => ['name' => 'NoCommerce', 'enabled' => true],
'SyliusAdminApiBundle' => ['name' => 'API', 'enabled' => false],
'SyliusAdminBundle' => ['name' => 'Admin', 'enabled' => false],
'SyliusShopBundle' => ['name' => 'Shop', 'enabled' => false],
],
'extensions' => [],
];

// If the NoCommerce Plugin is enabled we change some data
if ($this->featuresProvider->isNoCommerceEnabledForChannel()) {
$this->data['base_currency_code'] = NoCurrencyContext::NONE_CURRENCY_CODE;
$this->data['currency_code'] = NoCurrencyContext::NONE_CURRENCY_CODE;
$this->data['extensions']['MonsieurBizNoCommercePlugin'] = ['name' => 'NoCommerce', 'enabled' => true];
}

$this->data['extensions'] += [
'SyliusAdminApiBundle' => ['name' => 'API', 'enabled' => false],
'SyliusAdminBundle' => ['name' => 'Admin', 'enabled' => false],
'SyliusShopBundle' => ['name' => 'Shop', 'enabled' => false],
];

foreach (array_keys($this->data['extensions']) as $bundleName) {
Expand All @@ -55,7 +74,7 @@ public function __construct(

public function getVersion(): string
{
return $this->data['version'] . ' NoCommerce';
return $this->data['version'];
}

public function getExtensions(): array
Expand Down Expand Up @@ -86,9 +105,22 @@ public function getDefaultLocaleCode(): ?string
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.EmptyCatchBlock)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function collect(Request $request, Response $response, Throwable $exception = null): void
{
try {
/** @var ChannelInterface $channel */
$channel = $this->shopperContext->getChannel();

if (!$this->featuresProvider->isNoCommerceEnabledForChannel()) {
$baseCurrency = $channel->getBaseCurrency();
$this->data['base_currency_code'] = $baseCurrency ? $baseCurrency->getCode() : NoCurrencyContext::NONE_CURRENCY_CODE;
$this->data['currency_code'] = $this->shopperContext->getCurrencyCode();
}
} catch (ChannelNotFoundException|CurrencyNotFoundException $e) {
}

try {
$this->data['locale_code'] = $this->shopperContext->getLocaleCode();
} catch (LocaleNotFoundException $e) {
Expand Down
17 changes: 17 additions & 0 deletions src/Context/NoCurrencyContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,31 @@

namespace MonsieurBiz\SyliusNoCommercePlugin\Context;

use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProviderInterface;
use Sylius\Component\Currency\Context\CurrencyContextInterface;

final class NoCurrencyContext implements CurrencyContextInterface
{
public const NONE_CURRENCY_CODE = 'NONE';

private CurrencyContextInterface $decoratedCurrencyContext;

private FeaturesProviderInterface $featuresProvider;

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

public function getCurrencyCode(): string
{
if (!$this->featuresProvider->isNoCommerceEnabledForChannel()) {
return $this->decoratedCurrencyContext->getCurrencyCode();
}

return self::NONE_CURRENCY_CODE;
}
}
11 changes: 10 additions & 1 deletion src/EventListener/DisableFirewallListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace MonsieurBiz\SyliusNoCommercePlugin\EventListener;

use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProviderInterface;
use MonsieurBiz\SyliusSettingsPlugin\Settings\SettingsInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
Expand All @@ -31,14 +32,18 @@ final class DisableFirewallListener

private ChannelContextInterface $channelContext;

private FeaturesProviderInterface $featuresProvider;

public function __construct(
FirewallMap $firewallContext,
SettingsInterface $nocommerceSettings,
ChannelContextInterface $channelContext
ChannelContextInterface $channelContext,
FeaturesProviderInterface $featuresProvider
) {
$this->firewallContext = $firewallContext;
$this->nocommerceSettings = $nocommerceSettings;
$this->channelContext = $channelContext;
$this->featuresProvider = $featuresProvider;
}

/**
Expand All @@ -50,6 +55,10 @@ public function __invoke(RequestEvent $event): void
return;
}

if (!$this->featuresProvider->isNoCommerceEnabledForChannel()) {
return;
}

try {
$currentChannel = $this->channelContext->getChannel();
$disabledFirewall = (array) ($this->nocommerceSettings->getCurrentValue($currentChannel, null, 'disabled_firewall_contexts') ?: []);
Expand Down
32 changes: 32 additions & 0 deletions src/Form/Extension/ChannelTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,39 @@
namespace MonsieurBiz\SyliusNoCommercePlugin\Form\Extension;

use MonsieurBiz\SyliusNoCommercePlugin\Form\EventSubscriber\RemoveBaseCurrencySubscriber;
use MonsieurBiz\SyliusNoCommercePlugin\Provider\FeaturesProviderInterface;
use Sylius\Bundle\ChannelBundle\Form\Type\ChannelType;
use Sylius\Component\Core\Model\ChannelInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

final class ChannelTypeExtension extends AbstractTypeExtension
{
private FeaturesProviderInterface $featuresProvider;

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

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameters)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
/*
* We are testing with the channel that is currently edited to know which field to remove
* In case of a creation, the setting value is not set yet so the provider will check if the no commerce plugin is enabled in the global scope
*/
/** @var ChannelInterface $channel */
$channel = $options['data'];
if (!$this->featuresProvider->isNoCommerceEnabledForChannel($channel)) {
return;
}

$builder
->remove('shopBillingData')
->remove('menuTaxon')
Expand All @@ -42,4 +64,14 @@ public static function getExtendedTypes(): array
{
return [ChannelType::class];
}

public function buildView(FormView $view, FormInterface $form, array $options): void
{
parent::buildView($view, $form, $options);

/** @var ChannelInterface $channel */
$channel = $options['data'];

$view->vars['noCommerceEnabled'] = $this->featuresProvider->isNoCommerceEnabledForChannel($channel);
}
}
5 changes: 5 additions & 0 deletions src/Form/Type/Settings/NoCommerceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use MonsieurBiz\SyliusNoCommercePlugin\Firewall\RegistryInterface;
use MonsieurBiz\SyliusSettingsPlugin\Form\AbstractSettingsType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;

Expand Down Expand Up @@ -46,6 +47,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$choices[$firewallName] = $firewallConfig->getContext() ?? $firewallName;
}

$this->addWithDefaultCheckbox($builder, 'enabled', CheckboxType::class, [
'label' => 'monsieurbiz.nocommerce.ui.form.field.enabled.label',
'required' => false,
]);
$this->addWithDefaultCheckbox($builder, 'disabled_firewall_contexts', ChoiceType::class, [
'label' => 'monsieurbiz.nocommerce.ui.form.field.disabled_firewall_contexts.label',
'required' => false,
Expand Down
Loading

0 comments on commit b5a08c2

Please sign in to comment.