|
- <?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\Product\Product;
- use domain\Product\RotatingProduct\RotatingProduct;
- use domain\Product\RotatingProduct\RotatingProductRepository;
- use yii\base\ErrorException;
-
- class RotatingResolver extends AbstractResolver
- {
- protected DistributionRepository $distributionRepository;
- protected DistributionRotatingRepository $distributionRotatingRepository;
- protected RotatingProductRepository $rotatingProductRepository;
-
- public function loadDependencies(): void
- {
- $this->distributionRepository = $this->loadService(DistributionRepository::class);
- $this->distributionRotatingRepository = $this->loadService(DistributionRotatingRepository::class);
- $this->rotatingProductRepository = $this->loadService(RotatingProductRepository::class);
- }
-
- public function getPreviousDistribution(Rotating $rotating, \DateTime $date): ?Distribution
- {
- $date = $date->modify('previous '.$rotating->getDayAsString('en'));
- return $this->distributionRepository->findOneDistributionByDateTime($date);
- }
-
- public function getNextDistribution(Rotating $rotating, \DateTime $date): ?Distribution
- {
- $date = $date->modify('next '.$rotating->getDayAsString('en'));
- return $this->distributionRepository->findOneDistributionByDateTime($date);
- }
-
- /**
- * @throws ErrorException
- */
- public function deductRotatingProductOfDistribution(Rotating $rotating, Distribution $distribution): RotatingProduct
- {
- $date = clone $distribution->getDate();
- $previousDistribution = $this->getPreviousDistribution($rotating, $date);
- if($previousDistribution) {
- $previousDistributionRotating = $this->distributionRotatingRepository->findOneDistributionRotating($previousDistribution, $rotating);
-
- if($previousDistributionRotating) {
- $rotatingProductPreviousDistribution = $previousDistributionRotating->getRotatingProduct();
- $nextRotatingProduct = $this->getNextRotatingProduct($rotating, $rotatingProductPreviousDistribution);
- return $nextRotatingProduct;
- }
- }
-
- 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;
- break;
- }
-
- if($rotatingProductReturn) {
- return $rotatingProductReturn;
- }
- else {
- throw new ErrorException("Le produit tournant doit contenir au moins un produit");
- }
- }
-
- public function getRotatingOfProductInDistribution(Product $product, Distribution $distribution): ?Rotating
- {
- $distributionRotatingsArray = $this->distributionRotatingRepository->findDistributionRotatingsByDistribution($distribution);
- foreach($distributionRotatingsArray as $distributionRotating) {
- if($distributionRotating->getRotatingProduct()->getProduct()->equal($product)) {
- return $distributionRotating->getRotating();
- }
- }
-
- return null;
- }
-
- public function isRotatingOfProductInDistribution(Product $product, Distribution $distribution): bool
- {
- return (bool) $this->getRotatingOfProductInDistribution($product, $distribution);
- }
- }
|