|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?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");
- }
- }
- }
|