Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

45 lines
1.6KB

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