|
- <?php
-
- namespace domain\Product\Rotating;
-
- use domain\_\AbstractResolver;
- use domain\Distribution\Distribution\Distribution;
- use domain\Distribution\Distribution\DistributionRepository;
- use domain\Distribution\DistributionRotating\DistributionRotatingRepository;
- use domain\Product\RotatingProduct\RotatingProduct;
- use domain\Product\RotatingProduct\RotatingProductRepository;
- use yii\base\ErrorException;
-
- class RotatingResolver extends AbstractResolver
- {
- protected DistributionRepository $distributionRepository;
- protected DistributionRotatingRepository $distributionRotatingRepository;
-
- public function loadDependencies(): void
- {
- $this->distributionRepository = $this->loadService(DistributionRepository::class);
- $this->distributionRotatingRepository = $this->loadService(DistributionRotatingRepository::class);
- }
-
- public function getPreviousDistribution(Rotating $rotating, \DateTime $date): ?Distribution
- {
- $date = $date->modify('previous '.$rotating->getDayAsString());
- return $this->distributionRepository->findOneDistributionByDateTime($date);
- }
-
- public function getNextDistribution(Rotating $rotating, \DateTime $date): ?Distribution
- {
- $date = $date->modify('next '.$rotating->getDayAsString());
- return $this->distributionRepository->findOneDistributionByDateTime($date);
- }
-
- /**
- * @throws ErrorException
- */
- public function deductNextRotatingProductOfDistribution(Rotating $rotating, Distribution $distribution): RotatingProduct
- {
- $date = clone $distribution->getDate();
- $previousDistribution = $this->getPreviousDistribution($rotating, $date);
- $previousDistributionRotating = $this->distributionRotatingRepository->findOneDistributionRotating($previousDistribution, $rotating);
- if($previousDistributionRotating) {
- $rotatingProductPreviousDistribution = $previousDistributionRotating->getRotatingProduct();
- return $this->getNextRotatingProduct($rotating, $rotatingProductPreviousDistribution);
- }
-
- return $this->getFirstRotatingProduct($rotating);
- }
-
- /**
- * @throws ErrorException
- */
- public function getNextRotatingProduct(Rotating $rotating, RotatingProduct $currentRotatingProduct = null): RotatingProduct
- {
- if(is_null($currentRotatingProduct)) {
- return $this->getFirstRotatingProduct($rotating);
- }
-
- $rotatingProductReturn = null;
- $nextIsTheOne = false;
- foreach($rotating->getRotatingProducts() as $rotatingProduct) {
- if($nextIsTheOne) {
- return $rotatingProduct;
- }
- if($rotatingProduct->getId() == $currentRotatingProduct->getId()) {
- $nextIsTheOne = true;
- }
- }
-
- if($nextIsTheOne) {
- $rotatingProductReturn = $this->getFirstRotatingProduct($rotating);
- }
-
- if($rotatingProductReturn) {
- return $rotatingProductReturn;
- }
- else {
- throw new ErrorException("Le produit tournant suivant n'a pas été trouvé");
- }
- }
-
- /**
- * @throws ErrorException
- */
- public function getFirstRotatingProduct(Rotating $rotating): RotatingProduct
- {
- $rotatingProductReturn = null;
- foreach($rotating->getRotatingProducts() as $rotatingProduct) {
- $rotatingProductReturn = $rotatingProduct;
- }
-
- if($rotatingProductReturn) {
- return $rotatingProductReturn;
- }
- else {
- throw new ErrorException("Le produit tournant doit contenir au moins un produit");
- }
- }
- }
|