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.

AccessoryManager.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace domain\Product\Accessory;
  3. use domain\_\AbstractManager;
  4. use domain\Producer\Producer\Producer;
  5. use domain\Product\Product\ProductRepository;
  6. use domain\Product\ProductAccessory\ProductAccessoryManager;
  7. use domain\Product\ProductAccessory\ProductAccessoryRepository;
  8. use yii\base\ErrorException;
  9. class AccessoryManager extends AbstractManager
  10. {
  11. protected AccessoryBuilder $accessoryBuilder;
  12. protected ProductAccessoryManager $productAccessoryManager;
  13. protected ProductRepository $productRepository;
  14. public function loadDependencies(): void
  15. {
  16. $this->accessoryBuilder = $this->loadService(AccessoryBuilder::class);
  17. $this->productAccessoryManager = $this->loadService(ProductAccessoryManager::class);
  18. $this->productRepository = $this->loadService(ProductRepository::class);
  19. }
  20. public function createAccessory(Producer $producer, string $name, ?int $quantity): Accessory
  21. {
  22. $accessory = $this->accessoryBuilder->instanciateAccessory($producer);
  23. $accessory->setName($name);
  24. $accessory->setQuantity($quantity);
  25. $accessory->save();
  26. return $accessory;
  27. }
  28. public function manageProducts(Accessory $accessory, array $selectedProductsIds = []): void
  29. {
  30. $this->productAccessoryManager->deleteAllProductAccessoriesByAccessory($accessory);
  31. foreach($selectedProductsIds as $selectedProductId) {
  32. $product = $this->productRepository->findOneProductById($selectedProductId);
  33. if($product) {
  34. $this->productAccessoryManager->createProductAccessory($product, $accessory, 1);
  35. }
  36. }
  37. }
  38. }