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); } 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->manageRotatingProducts($rotating, $productsIdsArray); return $rotating; } /** * @throws ErrorException */ 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); } } // Rechargement obligatoire car rotatingProductsRelation a changé juste au-dessus // Possible de gérer ça via unset($rotating->rotatingProductsRelation) ? $rotating = $this->rotatingRepository->findOneRotatingById($rotating->getId()); // @TODO : gérer via événement $this->initializeRotatingInDistributionsIncoming($rotating); } /** * @throws ErrorException */ public function initializeRotatingInDistributionsIncoming(Rotating $rotating) { $date = new \DateTime(); $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date); $dateLastDistributionIncoming = $this->distributionResolver->getDateLastDistributionIncoming(); while($distribution && $dateLastDistributionIncoming && $date < $dateLastDistributionIncoming) { $this->initializeRotatingInDistribution($rotating, $distribution); $distribution = $this->rotatingResolver->getNextDistribution($rotating, $date); } } /** * @throws ErrorException */ public function initializeRotatingInDistribution(Rotating $rotating, Distribution $distribution) { $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(); } } }