No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

51 líneas
1.9KB

  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. }