Sfoglia il codice sorgente

Refactoring services #885

refactoring
Guillaume 1 anno fa
parent
commit
a2c0137d7a
6 ha cambiato i file con 55 aggiunte e 10 eliminazioni
  1. +8
    -0
      backend/controllers/BackendController.php
  2. +11
    -0
      common/components/BusinessLogic.php
  3. +2
    -0
      common/logic/AbstractService.php
  4. +7
    -8
      common/logic/Distribution/Distribution/DistributionBuilder.php
  5. +2
    -2
      common/logic/Distribution/Distribution/DistributionRepository.php
  6. +25
    -0
      common/logic/ProducerContextTrait.php

+ 8
- 0
backend/controllers/BackendController.php Vedi File

@@ -45,6 +45,14 @@ use common\controllers\CommonController;

class BackendController extends CommonController
{
public function beforeAction($event)
{
// Contexte producteur
$this->getLogic()->setProducerContext($this->getProducerCurrent());

return parent::beforeAction($event);
}

/**
* Vérifie si l'utilisateur a au moins saisi un produit et un point de vente.
* Si ce n'est pas le cas : redirection vers le tableau de bord.

+ 11
- 0
common/components/BusinessLogic.php Vedi File

@@ -12,6 +12,7 @@ use common\logic\Order\Order\OrderManager;
use common\logic\Order\OrderStatusHistory\OrderStatusHistoryContainer;
use common\logic\Order\ProductOrder\ProductOrderContainer;
use common\logic\PointSale\PointSale\PointSaleContainer;
use common\logic\Producer\Producer\Producer;
use common\logic\Producer\Producer\ProducerContainer;
use common\logic\Producer\ProducerPriceRange\ProducerPriceRangeContainer;
use common\logic\Product\Product\ProductContainer;
@@ -60,6 +61,16 @@ class BusinessLogic
];
}

public function setProducerContext(Producer $producer)
{
foreach($this->getContainers() as $container) {
foreach($container->getServices() as $serviceClass) {
$instanceService = $serviceClass::getInstance();
$instanceService->setProducerContext($producer);
}
}
}

/*
* Hiérarchie des apps
*/

+ 2
- 0
common/logic/AbstractService.php Vedi File

@@ -6,6 +6,8 @@ use yii\base\ErrorException;

abstract class AbstractService extends AbstractSingleton implements ServiceInterface
{
use ProducerContextTrait;

protected function getHierarchy(): array
{
return [

+ 7
- 8
common/logic/Distribution/Distribution/DistributionBuilder.php Vedi File

@@ -3,7 +3,6 @@
namespace common\logic\Distribution\Distribution;

use common\logic\AbstractBuilder;
use common\logic\BuilderInterface;
use common\logic\Distribution\PointSaleDistribution\PointSaleDistribution;
use common\logic\Distribution\PointSaleDistribution\PointSaleDistributionBuilder;
use common\logic\Distribution\PointSaleDistribution\PointSaleDistributionRepository;
@@ -45,11 +44,11 @@ class DistributionBuilder extends AbstractBuilder
$this->productOrderBuilder = $this->loadService(ProductOrderBuilder::class);
}

public function instanciateDistribution(Producer $producer, string $date, bool $delivery = true): Distribution
public function instanciateDistribution(string $date, bool $delivery = true): Distribution
{
$distribution = new Distribution();

$distribution->id_producer = $producer->id;
$distribution->populateProducer($this->getProducerContext());
$distribution->date = $date;
$distribution->delivery = (int) $delivery;

@@ -57,9 +56,9 @@ class DistributionBuilder extends AbstractBuilder
}

// initDistribution
public function createDistribution(Producer $producer, string $date, bool $delivery = true): Distribution
public function createDistribution(string $date, bool $delivery = true): Distribution
{
$distribution = $this->instanciateDistribution($producer, $date, $delivery);
$distribution = $this->instanciateDistribution($date, $delivery);

$this->saveCreate($distribution);

@@ -69,10 +68,10 @@ class DistributionBuilder extends AbstractBuilder
return $distribution;
}

public function createDistributionIfNotExist(Producer $producer, string $date, int $delivery = 1)
public function createDistributionIfNotExist(string $date, int $delivery = 1)
{
return $this->distributionRepository->findOneDistribution($producer, $date)
?? $this->createDistribution($producer, $date, $delivery);
return $this->distributionRepository->findOneDistribution($date)
?? $this->createDistribution($date, $delivery);
}

public function createPointSaleDistributions(Distribution $distribution): void

+ 2
- 2
common/logic/Distribution/Distribution/DistributionRepository.php Vedi File

@@ -38,11 +38,11 @@ class DistributionRepository extends AbstractRepository
]);
}

public function findOneDistribution(Producer $producer, string $date, bool $active = null): ?Distribution
public function findOneDistribution(string $date, bool $active = null): ?Distribution
{
$paramsDistribution = [
'date' => $date,
'distribution.id_producer' => $producer->id
'distribution.id_producer' => $this->getProducerContext()
];

if(!is_null($active)) {

+ 25
- 0
common/logic/ProducerContextTrait.php Vedi File

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

namespace common\logic;

use common\logic\Producer\Producer\Producer;
use yii\base\ErrorException;

trait ProducerContextTrait
{
protected ?Producer $producerContext = null;

public function setProducerContext(Producer $producer): void
{
$this->producerContext = $producer;
}

public function getProducerContext(): Producer
{
if(is_null($this->producerContext)) {
throw new ErrorException("Le contexte producteur n''est pas défini.");
}

return $this->producerContext;
}
}

Loading…
Annulla
Salva