|
- <?php
-
- namespace domain\Product\ProductAccessory;
-
- use domain\_\AbstractManager;
- use domain\Product\Accessory\Accessory;
- use domain\Product\Product\Product;
-
- class ProductAccessoryManager extends AbstractManager
- {
- protected ProductAccessoryBuilder $productAccessoryBuilder;
- protected ProductAccessoryRepository $productAccessoryRepository;
-
- public function loadDependencies(): void
- {
- $this->productAccessoryBuilder = $this->loadService(ProductAccessoryBuilder::class);
- $this->productAccessoryRepository = $this->loadService(ProductAccessoryRepository::class);
- }
-
- public function createProductAccessory(Product $product, Accessory $accessory, int $quantity): ProductAccessory
- {
- $productAccessory = $this->productAccessoryBuilder->instanciateProductAccessory();
- $productAccessory->setProduct($product);
- $productAccessory->setAccessory($accessory);
- $productAccessory->setQuantity($quantity);
- $productAccessory->save();
- return $productAccessory;
- }
-
- public function createProductAccessoryIfNotExist(Product $product, Accessory $accessory, int $quantity): ProductAccessory
- {
- $productAccessory = $this->productAccessoryRepository->findOneProductAccessory($product, $accessory);
- if($productAccessory) {
- $productAccessory->setQuantity($quantity);
- $productAccessory->save();
- }
- else {
- $productAccessory = $this->createProductAccessory($product, $accessory, $quantity);
- }
-
- return $productAccessory;
- }
-
- public function deleteAllProductAccessoriesByAccessory(Accessory $accessory): void
- {
- $productAccessoriesArray = $this->productAccessoryRepository->findProductAccessoriesByAccessory($accessory);
- foreach($productAccessoriesArray as $productAccessory) {
- $productAccessory->delete();
- }
- }
- }
|