|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?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();
- }
- }
- }
|