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.

48 lines
1.9KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Builder\Product;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Factory\Product\ProductFamilySectionPropertyFactory;
  5. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  6. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  7. use Lc\CaracoleBundle\Repository\Product\ProductFamilySectionPropertyStore;
  8. class ProductFamilySectionPropertyBuilder
  9. {
  10. protected EntityManagerInterface $entityManager;
  11. protected ProductFamilySectionPropertyFactory $productFamilySectionPropertyFactory;
  12. protected ProductFamilySectionPropertyStore $productFamilySectionPropertyStore;
  13. public function __construct(
  14. EntityManagerInterface $entityManager,
  15. ProductFamilySectionPropertyFactory $productFamilySectionPropertyFactory,
  16. ProductFamilySectionPropertyStore $productFamilySectionPropertyStore
  17. ) {
  18. $this->entityManager = $entityManager;
  19. $this->productFamilySectionPropertyFactory = $productFamilySectionPropertyFactory;
  20. $this->productFamilySectionPropertyStore = $productFamilySectionPropertyStore;
  21. }
  22. public function enable(ProductFamilyInterface $productFamily, SectionInterface $section): void
  23. {
  24. $productFamilySectionProperty = $this->productFamilySectionPropertyStore
  25. ->setSection($section)
  26. ->getOneByProductFamily($productFamily);
  27. if ($productFamilySectionProperty) {
  28. if (!$productFamilySectionProperty->getStatus()) {
  29. $productFamilySectionProperty->setStatus(1);
  30. $this->entityManager->update($productFamilySectionProperty);
  31. }
  32. } else {
  33. $productFamilySectionProperty = $this->productFamilySectionPropertyFactory->create($section, $productFamily);
  34. $productFamilySectionProperty->setStatus(1);
  35. $this->entityManager->create($productFamilySectionProperty);
  36. }
  37. $this->entityManager->flush();
  38. }
  39. }