Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

50 lines
2.0KB

  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, bool $flush = true): 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. if($flush) {
  38. $this->entityManager->flush();
  39. }
  40. }
  41. }