You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductAccessoryManager.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace domain\Product\ProductAccessory;
  3. use domain\_\AbstractManager;
  4. use domain\Product\Accessory\Accessory;
  5. use domain\Product\Product\Product;
  6. class ProductAccessoryManager extends AbstractManager
  7. {
  8. protected ProductAccessoryBuilder $productAccessoryBuilder;
  9. protected ProductAccessoryRepository $productAccessoryRepository;
  10. public function loadDependencies(): void
  11. {
  12. $this->productAccessoryBuilder = $this->loadService(ProductAccessoryBuilder::class);
  13. $this->productAccessoryRepository = $this->loadService(ProductAccessoryRepository::class);
  14. }
  15. public function createProductAccessory(Product $product, Accessory $accessory, int $quantity): ProductAccessory
  16. {
  17. $productAccessory = $this->productAccessoryBuilder->instanciateProductAccessory();
  18. $productAccessory->setProduct($product);
  19. $productAccessory->setAccessory($accessory);
  20. $productAccessory->setQuantity($quantity);
  21. $productAccessory->save();
  22. return $productAccessory;
  23. }
  24. public function createProductAccessoryIfNotExist(Product $product, Accessory $accessory, int $quantity): ProductAccessory
  25. {
  26. $productAccessory = $this->productAccessoryRepository->findOneProductAccessory($product, $accessory);
  27. if($productAccessory) {
  28. $productAccessory->setQuantity($quantity);
  29. $productAccessory->save();
  30. }
  31. else {
  32. $productAccessory = $this->createProductAccessory($product, $accessory, $quantity);
  33. }
  34. return $productAccessory;
  35. }
  36. public function deleteAllProductAccessoriesByAccessory(Accessory $accessory): void
  37. {
  38. $productAccessoriesArray = $this->productAccessoryRepository->findProductAccessoriesByAccessory($accessory);
  39. foreach($productAccessoriesArray as $productAccessory) {
  40. $productAccessory->delete();
  41. }
  42. }
  43. }