浏览代码

[Technique] Mise en place FeatureChecker

feature/souke
Guillaume Bourgeois 1年前
父节点
当前提交
34c6f8866f
共有 15 个文件被更改,包括 102 次插入72 次删除
  1. +5
    -5
      backend/controllers/FeatureAdminController.php
  2. +2
    -2
      backend/views/layouts/left.php
  3. +3
    -3
      backend/views/producer/update.php
  4. +10
    -0
      common/logic/AbstractChecker.php
  5. +1
    -0
      common/logic/AbstractService.php
  6. +8
    -0
      common/logic/CheckerInterface.php
  7. +4
    -3
      common/logic/Distribution/Distribution/Export/DistributionShoppingCartLabelsPdfGenerator.php
  8. +7
    -0
      common/logic/Feature/Feature/Module/FeatureModule.php
  9. +49
    -0
      common/logic/Feature/Feature/Service/FeatureChecker.php
  10. +5
    -41
      common/logic/Feature/Feature/Service/FeatureManager.php
  11. +0
    -10
      common/logic/Feature/FeatureProducer/Service/FeatureProducerDefinition.php
  12. +4
    -4
      producer/controllers/CreditController.php
  13. +1
    -1
      producer/controllers/SiteController.php
  14. +2
    -2
      producer/views/credit/history.php
  15. +1
    -1
      producer/views/layouts/main.php

+ 5
- 5
backend/controllers/FeatureAdminController.php 查看文件

@@ -85,11 +85,11 @@ class FeatureAdminController extends BackendController
$feature = $this->findModel($id);

if($status) {
$featureManager->enable($feature);
$featureManager->enableFeature($feature);
$messageResponse = 'La fonctionnalité "'.Html::encode($feature->name).'" a bien été activée';
}
else {
$featureManager->disable($feature);
$featureManager->disableFeature($feature);
$messageResponse = 'La fonctionnalité "'.Html::encode($feature->name).'" a bien été désactivée';
}

@@ -102,13 +102,13 @@ class FeatureAdminController extends BackendController
$feature = $this->findModel($id);

if(is_null($status)) {
$featureManager->defaultForProducer($feature);
$featureManager->defaultFeatureForProducer($feature);
}
elseif($status == 0) {
$featureManager->disableForProducer($feature);
$featureManager->disableFeatureForProducer($feature);
}
elseif($status == 1) {
$featureManager->enableForProducer($feature);
$featureManager->enableFeatureForProducer($feature);
}

return $this->redirectReferer();

+ 2
- 2
backend/views/layouts/left.php 查看文件

@@ -44,7 +44,7 @@ $producerModule = $this->getProducerModule();
$userModule = UserModule::getInstance();
$userProducerModule = $this->getUserProducerModule();
$ticketModule = $this->getTicketModule();
$featureManager = $this->getFeatureModule()->getManager();
$featureChecker = $this->getFeatureModule()->getChecker();

$producer = GlobalParam::getCurrentProducer();
$userCurrent = GlobalParam::getCurrentUser();
@@ -106,7 +106,7 @@ $isUserCurrentGrantedAsProducer = $userModule->getAuthorizationChecker()->isGran
'items' => [
['label' => 'Liste', 'icon' => 'th-list', 'url' => ['/product/index'], 'visible' => $isUserCurrentGrantedAsProducer],
['label' => 'Catégories', 'icon' => 'book', 'url' => ['/product-category/index'], 'visible' => $isUserCurrentGrantedAsProducer],
['label' => 'Import prix', 'icon' => 'upload', 'url' => ['/product/price-import'], 'visible' => $isUserCurrentGrantedAsProducer && $featureManager->isEnabled(Feature::ALIAS_PRODUCT_PRICE_IMPORT)],
['label' => 'Import prix', 'icon' => 'upload', 'url' => ['/product/price-import'], 'visible' => $isUserCurrentGrantedAsProducer && $featureChecker->isEnabled(Feature::ALIAS_PRODUCT_PRICE_IMPORT)],
]
],
['label' => 'Points de vente', 'icon' => 'map-marker', 'url' => ['/point-sale/index'], 'visible' => $isUserCurrentGrantedAsProducer, 'active' => Yii::$app->controller->id == 'point-sale'],

+ 3
- 3
backend/views/producer/update.php 查看文件

@@ -56,7 +56,7 @@ use yii\helpers\ArrayHelper;
$userModule = UserModule::getInstance();
$userGroupModule = UserGroupModule::getInstance();
$distributionExportManager = DistributionModule::getInstance()->getExportManager();
$featureManager = FeatureModule::getInstance()->getManager();
$featureChecker = FeatureModule::getInstance()->getChecker();

$userCurrent = GlobalParam::getCurrentUser();

@@ -308,7 +308,7 @@ $this->addBreadcrumb($this->getTitle());
->dropDownList(Dropdown::noYesChoices()); ?>
<?= $form->field($model, 'option_csv_export_by_piece')
->dropDownList(Dropdown::noYesChoices()); ?>
<?php if($featureManager->isEnabled(Feature::ALIAS_EXPORT_SHOPPING_CART_LABELS_ADVANCED)): ?>
<?php if($featureChecker->isEnabled(Feature::ALIAS_EXPORT_SHOPPING_CART_LABELS_ADVANCED)): ?>
<?= $form->field($model, 'export_shopping_cart_labels_format')
->dropDownList($distributionExportManager->getGenerator(ExportManager::SHOPPING_CART_LABELS_PDF)->populateDropdownSpecificFormats()); ?>
<?php else: ?>
@@ -359,7 +359,7 @@ $this->addBreadcrumb($this->getTitle());
<?= $form->field($model, 'option_check_by_default_prevent_user_credit')
->dropDownList(Dropdown::noYesChoices()); ?>

<?php if($featureManager->isEnabled(Feature::ALIAS_ONLINE_PAYMENT)): ?>
<?php if($featureChecker->isEnabled(Feature::ALIAS_ONLINE_PAYMENT)): ?>
<h4>Paiement en ligne</h4>
<?php if($userModule->getAuthorizationChecker()->isGrantedAsAdministrator($userCurrent)): ?>
<?= $form->field($model, 'online_payment')

+ 10
- 0
common/logic/AbstractChecker.php 查看文件

@@ -0,0 +1,10 @@
<?php

namespace common\logic;

use yii\db\ActiveRecord;

abstract class AbstractChecker extends AbstractService implements CheckerInterface
{

}

+ 1
- 0
common/logic/AbstractService.php 查看文件

@@ -15,6 +15,7 @@ abstract class AbstractService extends AbstractSingleton implements ServiceInter
SolverInterface::class,
RepositoryQueryInterface::class,
RepositoryInterface::class,
CheckerInterface::class,
NotifierInterface::class,
BuilderInterface::class,
ResolverInterface::class,

+ 8
- 0
common/logic/CheckerInterface.php 查看文件

@@ -0,0 +1,8 @@
<?php

namespace common\logic;

interface CheckerInterface
{

}

+ 4
- 3
common/logic/Distribution/Distribution/Export/DistributionShoppingCartLabelsPdfGenerator.php 查看文件

@@ -5,6 +5,7 @@ namespace common\logic\Distribution\Distribution\Export;
use common\logic\AbstractGenerator;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Feature\Feature\Model\Feature;
use common\logic\Feature\Feature\Service\FeatureChecker;
use common\logic\Feature\Feature\Service\FeatureManager;
use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Repository\OrderRepository;
@@ -22,12 +23,14 @@ class DistributionShoppingCartLabelsPdfGenerator extends AbstractGenerator imple
protected ProducerSolver $producerSolver;
protected OrderRepository $orderRepository;
protected OrderSolver $orderSolver;
protected FeatureChecker $featureChecker;

public function loadDependencies(): void
{
$this->producerSolver = $this->loadService(ProducerSolver::class);
$this->orderRepository = $this->loadService(OrderRepository::class);
$this->orderSolver = $this->loadService(OrderSolver::class);
$this->featureChecker = $this->loadService(FeatureChecker::class);
}

public function getSpecificFormatDetailsArray(): array
@@ -39,11 +42,9 @@ class DistributionShoppingCartLabelsPdfGenerator extends AbstractGenerator imple

public function generate(Distribution $distribution, bool $save = false)
{
// @TODO : obligé de charger FeatureManager comme ça car on est dans un service Generator (problème de hiérarchie de services)
$featureManager = FeatureManager::getInstance();
$isSpecificFormat = false;
$exportShoppingCartLabelsFormat = $this->producerSolver->getConfig('export_shopping_cart_labels_format');
if($featureManager->isEnabled(Feature::ALIAS_EXPORT_SHOPPING_CART_LABELS_ADVANCED)) {
if($this->featureChecker->isEnabled(Feature::ALIAS_EXPORT_SHOPPING_CART_LABELS_ADVANCED)) {
if($exportShoppingCartLabelsFormat) {
$isSpecificFormat = true;
[$specificFormatWidth, $specificFormatHeight] = $this->getSpecificFormatDetails($exportShoppingCartLabelsFormat);

+ 7
- 0
common/logic/Feature/Feature/Module/FeatureModule.php 查看文件

@@ -5,6 +5,7 @@ namespace common\logic\Feature\Feature\Module;
use common\logic\AbstractModule;
use common\logic\Feature\Feature\Repository\FeatureRepository;
use common\logic\Feature\Feature\Service\FeatureBuilder;
use common\logic\Feature\Feature\Service\FeatureChecker;
use common\logic\Feature\Feature\Service\FeatureDefinition;
use common\logic\Feature\Feature\Service\FeatureImporter;
use common\logic\Feature\Feature\Service\FeatureManager;
@@ -16,6 +17,7 @@ class FeatureModule extends AbstractModule
return [
FeatureDefinition::class,
FeatureRepository::class,
FeatureChecker::class,
FeatureBuilder::class,
FeatureImporter::class,
FeatureManager::class
@@ -32,6 +34,11 @@ class FeatureModule extends AbstractModule
return FeatureRepository::getInstance();
}

public function getChecker(): FeatureChecker
{
return FeatureChecker::getInstance();
}

public function getBuilder(): FeatureBuilder
{
return FeatureBuilder::getInstance();

+ 49
- 0
common/logic/Feature/Feature/Service/FeatureChecker.php 查看文件

@@ -0,0 +1,49 @@
<?php

namespace common\logic\Feature\Feature\Service;

use common\logic\AbstractChecker;
use common\logic\Feature\Feature\Repository\FeatureRepository;
use common\logic\Feature\FeatureProducer\Repository\FeatureProducerRepository;
use yii\base\ErrorException;

class FeatureChecker extends AbstractChecker
{
protected FeatureRepository $featureRepository;
protected FeatureProducerRepository $featureProducerRepository;

public function loadDependencies(): void
{
$this->featureRepository = $this->loadService(FeatureRepository::class);
$this->featureProducerRepository = $this->loadService(FeatureProducerRepository::class);
}

public function isEnabled(string $aliasFeature): bool
{
$feature = $this->featureRepository->findOneFeatureByAlias($aliasFeature);
if(!$feature) {
throw new ErrorException("Fonctionnalité avec l'alias '".$aliasFeature."' non trouvée");
}

if(!$feature->status) {
return false;
}

$featureProducer = $this->featureProducerRepository->findOneFeatureProducer($feature);
if(!$featureProducer || is_null($featureProducer->status)) {
if($feature->is_paid_feature || $feature->only_for_selected_producers) {
return false;
}

return $feature->status;
}
else {
return $featureProducer->status;
}
}

public function isDisabled(string $aliasFeature): bool
{
return !$this->isEnabled($aliasFeature);
}
}

+ 5
- 41
common/logic/Feature/Feature/Service/FeatureManager.php 查看文件

@@ -4,76 +4,40 @@ namespace common\logic\Feature\Feature\Service;

use common\logic\AbstractManager;
use common\logic\Feature\Feature\Model\Feature;
use common\logic\Feature\Feature\Repository\FeatureRepository;
use common\logic\Feature\FeatureProducer\Repository\FeatureProducerRepository;
use common\logic\Feature\FeatureProducer\Service\FeatureProducerBuilder;
use yii\base\ErrorException;

class FeatureManager extends AbstractManager
{
protected FeatureRepository $featureRepository;
protected FeatureProducerRepository $featureProducerRepository;
protected FeatureBuilder $featureBuilder;
protected FeatureProducerBuilder $featureProducerBuilder;

public function loadDependencies(): void
{
$this->featureRepository = $this->loadService(FeatureRepository::class);
$this->featureProducerRepository = $this->loadService(FeatureProducerRepository::class);
$this->featureBuilder = $this->loadService(FeatureBuilder::class);
$this->featureProducerBuilder = $this->loadService(FeatureProducerBuilder::class);
}

public function isEnabled(string $aliasFeature): bool
{
$feature = $this->featureRepository->findOneFeatureByAlias($aliasFeature);
if(!$feature) {
throw new ErrorException("Fonctionnalité avec l'alias '".$aliasFeature."' non trouvée");
}

if(!$feature->status) {
return false;
}

$featureProducer = $this->featureProducerRepository->findOneFeatureProducer($feature);
if(!$featureProducer || is_null($featureProducer->status)) {
if($feature->is_paid_feature || $feature->only_for_selected_producers) {
return false;
}

return $feature->status;
}
else {
return $featureProducer->status;
}
}

public function isDisabled(string $aliasFeature): bool
{
return !$this->isEnabled($aliasFeature);
}

public function enable(Feature $feature): void
public function enableFeature(Feature $feature): void
{
$this->featureBuilder->updateStatus($feature, true);
}

public function disable(Feature $feature): void
public function disableFeature(Feature $feature): void
{
$this->featureBuilder->updateStatus($feature, false);
}

public function enableForProducer(Feature $feature)
public function enableFeatureForProducer(Feature $feature)
{
$this->featureProducerBuilder->updateStatusByFeature($feature, true);
}

public function disableForProducer(Feature $feature)
public function disableFeatureForProducer(Feature $feature)
{
$this->featureProducerBuilder->updateStatusByFeature($feature, false);
}

public function defaultForProducer(Feature $feature)
public function defaultFeatureForProducer(Feature $feature)
{
$this->featureProducerBuilder->updateStatusByFeature($feature, null);
}

+ 0
- 10
common/logic/Feature/FeatureProducer/Service/FeatureProducerDefinition.php 查看文件

@@ -12,14 +12,4 @@ class FeatureProducerDefinition extends AbstractDefinition
{
return FeatureProducer::class;
}

public function getDefinition(): FeatureProducerDefinition
{
return FeatureProducerDefinition::getInstance();
}

public function getRepository(): FeatureProducerRepository
{
return FeatureProducerRepository::getInstance();
}
}

+ 4
- 4
producer/controllers/CreditController.php 查看文件

@@ -109,13 +109,13 @@ class CreditController extends ProducerBaseController

public function actionAdd()
{
$featureManager = $this->getFeatureModule()->getManager();
$featureChecker = $this->getFeatureModule()->getChecker();
$producer = $this->getProducerCurrent();
if (\Yii::$app->user->isGuest) {
return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $producer->id]));
}

if ($featureManager->isEnabled(Feature::ALIAS_ONLINE_PAYMENT)
if ($featureChecker->isEnabled(Feature::ALIAS_ONLINE_PAYMENT)
&& ($producer->online_payment || $producer->option_stripe_mode_test)) {

$creditForm = new CreditForm;
@@ -183,9 +183,9 @@ class CreditController extends ProducerBaseController
$paymentManager = $this->getPaymentModule();
$producerModule = $this->getProducerModule();
$userModule = $this->getUserModule();
$featureManager = $this->getFeatureModule()->getManager();
$featureChecker = $this->getFeatureModule()->getChecker();

if ($featureManager->isEnabled(Feature::ALIAS_ONLINE_PAYMENT)) {
if ($featureChecker->isEnabled(Feature::ALIAS_ONLINE_PAYMENT)) {
$producer = $this->getProducerCurrent();
$contactProducer = $producerModule->getMainContact($producer);


+ 1
- 1
producer/controllers/SiteController.php 查看文件

@@ -180,7 +180,7 @@ class SiteController extends ProducerBaseController
public function actionContact()
{
$featureModule = $this->getFeatureModule();
if($featureModule->getManager()->isDisabled(Feature::ALIAS_CONTACT)) {
if($featureModule->getChecker()->isDisabled(Feature::ALIAS_CONTACT)) {
return $this->redirect(['site/index']);
}


+ 2
- 2
producer/views/credit/history.php 查看文件

@@ -42,12 +42,12 @@ use common\logic\Payment\Module\PaymentModule;
use yii\grid\GridView;

$paymentManager = PaymentModule::getInstance();
$featureManager = FeatureModule::getInstance();
$featureChecker = FeatureModule::getInstance()->getChecker();
$producer = $this->context->getProducerCurrent();
$this->setTitle('Crédit : <span id="credit-user">' . number_format($creditUser, 2) . ' €</span>');
$this->setPageTitle('Crédit');

if ($featureManager->isEnabled(Feature::ALIAS_ONLINE_PAYMENT) && $this->context->getProducerCurrent()->online_payment) {
if ($featureChecker->isEnabled(Feature::ALIAS_ONLINE_PAYMENT) && $this->context->getProducerCurrent()->online_payment) {
$this->addButton(
[
'label' => '<span class="glyphicon glyphicon-credit-card"></span> Créditer mon compte',

+ 1
- 1
producer/views/layouts/main.php 查看文件

@@ -181,7 +181,7 @@ if (!Yii::$app->user->isGuest) {
'label' => '<span class="glyphicon glyphicon-envelope"></span> Contact',
'url' => $this->getUrlManagerProducer()->createUrl(['site/contact']),
'active' => $this->getControllerAction() == 'site/contact',
'visible' => $featureModule->getManager()->isEnabled(Feature::ALIAS_CONTACT)
'visible' => $featureModule->getChecker()->isEnabled(Feature::ALIAS_CONTACT)
],
],
]);

正在加载...
取消
保存