Browse Source

Refactoring services #885

refactoring
Guillaume 1 year ago
parent
commit
a2c0137d7a
6 changed files with 55 additions and 10 deletions
  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 View File



class BackendController extends 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. * 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. * Si ce n'est pas le cas : redirection vers le tableau de bord.

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

use common\logic\Order\OrderStatusHistory\OrderStatusHistoryContainer; use common\logic\Order\OrderStatusHistory\OrderStatusHistoryContainer;
use common\logic\Order\ProductOrder\ProductOrderContainer; use common\logic\Order\ProductOrder\ProductOrderContainer;
use common\logic\PointSale\PointSale\PointSaleContainer; use common\logic\PointSale\PointSale\PointSaleContainer;
use common\logic\Producer\Producer\Producer;
use common\logic\Producer\Producer\ProducerContainer; use common\logic\Producer\Producer\ProducerContainer;
use common\logic\Producer\ProducerPriceRange\ProducerPriceRangeContainer; use common\logic\Producer\ProducerPriceRange\ProducerPriceRangeContainer;
use common\logic\Product\Product\ProductContainer; use common\logic\Product\Product\ProductContainer;
]; ];
} }


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 * Hiérarchie des apps
*/ */

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



abstract class AbstractService extends AbstractSingleton implements ServiceInterface abstract class AbstractService extends AbstractSingleton implements ServiceInterface
{ {
use ProducerContextTrait;

protected function getHierarchy(): array protected function getHierarchy(): array
{ {
return [ return [

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

namespace common\logic\Distribution\Distribution; namespace common\logic\Distribution\Distribution;


use common\logic\AbstractBuilder; use common\logic\AbstractBuilder;
use common\logic\BuilderInterface;
use common\logic\Distribution\PointSaleDistribution\PointSaleDistribution; use common\logic\Distribution\PointSaleDistribution\PointSaleDistribution;
use common\logic\Distribution\PointSaleDistribution\PointSaleDistributionBuilder; use common\logic\Distribution\PointSaleDistribution\PointSaleDistributionBuilder;
use common\logic\Distribution\PointSaleDistribution\PointSaleDistributionRepository; use common\logic\Distribution\PointSaleDistribution\PointSaleDistributionRepository;
$this->productOrderBuilder = $this->loadService(ProductOrderBuilder::class); $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 = new Distribution();


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


} }


// initDistribution // 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); $this->saveCreate($distribution);


return $distribution; 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 public function createPointSaleDistributions(Distribution $distribution): void

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

]); ]);
} }


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


if(!is_null($active)) { if(!is_null($active)) {

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

<?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…
Cancel
Save