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.

51 lines
2.2KB

  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. use Lc\CaracoleBundle\Solver\Product\ProductFamilySectionPropertySolver;
  9. class ProductFamilySectionPropertyBuilder
  10. {
  11. protected EntityManagerInterface $entityManager;
  12. protected ProductFamilySectionPropertyFactory $productFamilySectionPropertyFactory;
  13. protected ProductFamilySectionPropertySolver $productFamilySectionPropertySolver;
  14. public function __construct(
  15. EntityManagerInterface $entityManager,
  16. ProductFamilySectionPropertyFactory $productFamilySectionPropertyFactory,
  17. ProductFamilySectionPropertySolver $productFamilySectionPropertySolver
  18. ) {
  19. $this->entityManager = $entityManager;
  20. $this->productFamilySectionPropertyFactory = $productFamilySectionPropertyFactory;
  21. $this->productFamilySectionPropertySolver = $productFamilySectionPropertySolver;
  22. }
  23. public function enable(ProductFamilyInterface $productFamily, SectionInterface $section, bool $flush = true): void
  24. {
  25. $productFamilySectionProperty = $this->productFamilySectionPropertySolver->getProductFamilySectionProperty($productFamily, $section);
  26. if ($productFamilySectionProperty) {
  27. if (!$productFamilySectionProperty->getStatus()) {
  28. $productFamilySectionProperty->setStatus(1);
  29. $this->entityManager->update($productFamilySectionProperty);
  30. }
  31. } else {
  32. $productFamilySectionProperty = $this->productFamilySectionPropertyFactory->create($section, $productFamily);
  33. $productFamilySectionProperty->setStatus(1);
  34. $productFamily->addProductFamilySectionProperty($productFamilySectionProperty);
  35. $this->entityManager->create($productFamilySectionProperty);
  36. $this->entityManager->update($productFamily);
  37. }
  38. if($flush) {
  39. $this->entityManager->flush();
  40. }
  41. }
  42. }