|
- <?php
-
- namespace domain\Product\Rotating;
-
- use domain\_\AbstractManager;
- use domain\Distribution\Distribution\Distribution;
- use domain\Distribution\Distribution\DistributionRepository;
- use domain\Distribution\Distribution\DistributionResolver;
- use domain\Distribution\DistributionRotating\DistributionRotatingManager;
- use domain\Producer\Producer\Producer;
- use domain\Product\Product\ProductManager;
- use domain\Product\Product\ProductRepository;
- use domain\Product\Rotating\Event\RotatingSaveEvent;
- use domain\Product\RotatingProduct\RotatingProductManager;
- use yii\base\ErrorException;
-
- class RotatingManager extends AbstractManager
- {
- protected RotatingBuilder $rotatingBuilder;
- protected ProductRepository $productRepository;
- protected RotatingProductManager $rotatingProductManager;
- protected DistributionRepository $distributionRepository;
- protected DistributionRotatingManager $distributionRotatingManager;
- protected RotatingResolver $rotatingResolver;
- protected DistributionResolver $distributionResolver;
- protected RotatingRepository $rotatingRepository;
- protected ProductManager $productManager;
-
- /**
- * @throws ErrorException
- */
- public function loadDependencies(): void
- {
- $this->rotatingBuilder = $this->loadService(RotatingBuilder::class);
- $this->productRepository = $this->loadService(ProductRepository::class);
- $this->rotatingProductManager = $this->loadService(RotatingProductManager::class);
- $this->distributionRepository = $this->loadService(DistributionRepository::class);
- $this->distributionRotatingManager = $this->loadService(DistributionRotatingManager::class);
- $this->rotatingResolver = $this->loadService(RotatingResolver::class);
- $this->distributionResolver = $this->loadService(DistributionResolver::class);
- $this->rotatingRepository = $this->loadService(RotatingRepository::class);
- $this->productManager = $this->loadService(ProductManager::class);
- }
-
- /**
- * @throws ErrorException
- */
- public function createRotating(Producer $producer, string $name, int $day, array $productsIdsArray): Rotating
- {
- $rotating = $this->rotatingBuilder->instanciateRotating($producer);
- $rotating->setName($name);
- $rotating->setDay($day);
- $rotating->save();
- $this->afterSaveRotating($rotating, $productsIdsArray);
-
- return $rotating;
- }
-
- public function updateRotating(Rotating $rotating, array $productsIdsArray): bool
- {
- $return = $rotating->save();
- $this->afterSaveRotating($rotating, $productsIdsArray);
-
- return $return;
- }
-
- public function afterSaveRotating(Rotating $rotating, array $productsIdsArray)
- {
- $this->manageRotatingProducts($rotating, $productsIdsArray);
-
- $rotating->triggerEvent(RotatingSaveEvent::class, [
- 'rotating' => $rotating
- ]);
- }
-
- public function manageRotatingProducts(Rotating $rotating, array $productsIdsArray = []): void
- {
- $this->deleteAllRotatingProducts($rotating);
-
- foreach($productsIdsArray as $productId) {
- $product = $this->productRepository->findOneProductById($productId);
- if($product) {
- $this->rotatingProductManager->createRotatingProduct($rotating, $product);
- }
- }
- }
-
- /**
- * @throws ErrorException
- */
- public function initializeRotatingInDistributionsIncoming(Rotating $rotating)
- {
- $date = new \DateTime();
- $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
- $dateLastDistributionIncoming = $this->distributionResolver->getDateLastDistributionIncoming();
-
- if($dateLastDistributionIncoming) {
- while($date < $dateLastDistributionIncoming) {
- if($distribution) {
- $this->initializeRotatingInDistribution($rotating, $distribution);
- }
- $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date);
- }
- }
- }
-
- /**
- * @throws ErrorException
- */
- public function initializeRotatingInDistribution(Rotating $rotating, Distribution $distribution)
- {
- if($rotating->getDay() == $distribution->getDate()->format('N')) {
- $rotatingProductOfDistribution = $this->rotatingResolver->deductRotatingProductOfDistribution($rotating, $distribution);
-
- $this->distributionRotatingManager->createDistributionRotatingIfNotExist(
- $distribution,
- $rotatingProductOfDistribution
- );
-
- foreach($rotating->getRotatingProducts() as $rotatingProduct) {
- if($rotatingProduct->getId() == $rotatingProductOfDistribution->getId()) {
- $this->productManager->activateProductInDistribution($rotatingProductOfDistribution->getProduct(), $distribution);
- }
- else {
- $this->productManager->deactivateProductInDistribution($rotatingProduct->getProduct(), $distribution);
- }
- }
- }
- }
-
- /**
- * @throws ErrorException
- */
- public function initializeAllRotatingsInDistribution(Distribution $distribution)
- {
- $rotatingsArray = $this->rotatingRepository->findRotatings();
-
- foreach($rotatingsArray as $rotating) {
- $this->initializeRotatingInDistribution($rotating, $distribution);
- }
- }
-
- public function deleteRotating(Rotating $rotating): bool
- {
- $this->deleteAllRotatingProducts($rotating);
- return $rotating->delete();
- }
-
- public function deleteAllRotatingProducts(Rotating $rotating): void
- {
- foreach($rotating->getDistributionRotatings() as $distributionRotating) {
- $distributionRotating->delete();
- }
-
- foreach($rotating->getRotatingProducts() as $rotatingProduct) {
- $rotatingProduct->delete();
- }
- }
- }
|