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.

71 line
3.1KB

  1. <?php
  2. namespace Lc\CaracoleBundle\EventSubscriber\Product;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Container\Product\ProductCategoryContainer;
  5. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  6. use Lc\CaracoleBundle\Model\Product\ProductFamilySectionPropertyInterface;
  7. use Lc\CaracoleBundle\Solver\Product\ProductCategorySolver;
  8. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class OnlineOrOfflineProductCategoryAfterProductFamilyEventSubscriber implements EventSubscriberInterface
  11. {
  12. protected EntityManagerInterface $entityManager;
  13. protected ProductCategoryContainer $productCategoryContainer;
  14. public function __construct(EntityManagerInterface $entityManager, ProductCategoryContainer $productCategoryContainer)
  15. {
  16. $this->entityManager = $entityManager;
  17. $this->productCategoryContainer = $productCategoryContainer;
  18. }
  19. public static function getSubscribedEvents()
  20. {
  21. return [
  22. EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
  23. EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
  24. ];
  25. }
  26. public function processBeforePersistProductFamilySectionInterface(EntityManagerEvent $event)
  27. {
  28. if ($event->getEntity() instanceof ProductFamilySectionPropertyInterface) {
  29. $this->setProductCategoryByProductFamilySectionProperty($event->getEntity());
  30. } else if ($event->getEntity() instanceof ProductFamilyInterface) {
  31. foreach ($event->getEntity()->getProductFamilySectionProperties() as $productFamilySectionProperty) {
  32. $this->setProductCategoryByProductFamilySectionProperty($productFamilySectionProperty);
  33. }
  34. }
  35. }
  36. protected function setProductCategoryByProductFamilySectionProperty($productFamilySectionProperty)
  37. {
  38. $productFamily = $productFamilySectionProperty->getProductFamily();
  39. if ($productFamilySectionProperty->getStatus() == 1 && $productFamily->getStatus() == 1) {
  40. $section = $productFamilySectionProperty->getSection();
  41. $productCategoryArray = $productFamilySectionProperty->getProductFamily()->getProductCategories();
  42. foreach ($productCategoryArray as $productCategory) {
  43. if ($productCategory->getSection() === $section) {
  44. $this->productCategoryContainer->getBuilder()->setOnlineIfOnlineProductfamily($productCategory);
  45. }
  46. }
  47. } else if ($productFamilySectionProperty->getStatus() == 0 || $productFamily->getStatus() == 0) {
  48. $section = $productFamilySectionProperty->getSection();
  49. $productCategoryArray = $productFamilySectionProperty->getProductFamily()->getProductCategories();
  50. foreach ($productCategoryArray as $productCategory) {
  51. if ($productCategory->getSection() === $section) {
  52. $this->productCategoryContainer->getBuilder()->setOfflineIfOfflineProductfamily($productCategory);
  53. }
  54. }
  55. }
  56. }
  57. }