|
- <?php
-
- namespace domain\Product\Accessory;
-
- use domain\_\AbstractManager;
- use domain\Producer\Producer\Producer;
- use domain\Product\Product\ProductRepository;
- use domain\Product\ProductAccessory\ProductAccessoryManager;
- use domain\Product\ProductAccessory\ProductAccessoryRepository;
- use yii\base\ErrorException;
-
- class AccessoryManager extends AbstractManager
- {
- protected AccessoryBuilder $accessoryBuilder;
- protected ProductAccessoryManager $productAccessoryManager;
- protected ProductRepository $productRepository;
-
- public function loadDependencies(): void
- {
- $this->accessoryBuilder = $this->loadService(AccessoryBuilder::class);
- $this->productAccessoryManager = $this->loadService(ProductAccessoryManager::class);
- $this->productRepository = $this->loadService(ProductRepository::class);
- }
-
- public function createAccessory(Producer $producer, string $name, ?int $quantity): Accessory
- {
- $accessory = $this->accessoryBuilder->instanciateAccessory($producer);
- $accessory->setName($name);
- $accessory->setQuantity($quantity);
- $accessory->save();
- return $accessory;
- }
-
- public function manageProducts(Accessory $accessory, array $selectedProductsIds = []): void
- {
- $this->productAccessoryManager->deleteAllProductAccessoriesByAccessory($accessory);
-
- foreach($selectedProductsIds as $selectedProductId) {
- $product = $this->productRepository->findOneProductById($selectedProductId);
- if($product) {
- $this->productAccessoryManager->createProductAccessory($product, $accessory, 1);
- }
- }
- }
-
- public function deleteAccessory(Accessory $accessory): bool
- {
- foreach($accessory->getProductAccessories() as $productAccessory) {
- $productAccessory->delete();
- }
-
- return $accessory->delete();
- }
- }
|