|
- <?php
-
- namespace Lc\CaracoleBundle\EventSubscriber\Product;
-
- use Doctrine\ORM\EntityManagerInterface;
-
- use Lc\CaracoleBundle\Container\Product\ProductCategoryContainer;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
- use Lc\CaracoleBundle\Model\Product\ProductFamilySectionPropertyInterface;
- use Lc\CaracoleBundle\Solver\Product\ProductCategorySolver;
- use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-
- class OnlineOrOfflineProductCategoryAfterProductFamilyEventSubscriber implements EventSubscriberInterface
- {
- protected EntityManagerInterface $entityManager;
- protected ProductCategoryContainer $productCategoryContainer;
-
- public function __construct(EntityManagerInterface $entityManager, ProductCategoryContainer $productCategoryContainer)
- {
- $this->entityManager = $entityManager;
- $this->productCategoryContainer = $productCategoryContainer;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
- EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamilySectionInterface'],
- ];
- }
-
- public function processBeforePersistProductFamilySectionInterface(EntityManagerEvent $event)
- {
- if ($event->getEntity() instanceof ProductFamilySectionPropertyInterface) {
- $this->setProductCategoryByProductFamilySectionProperty($event->getEntity());
- } else if ($event->getEntity() instanceof ProductFamilyInterface) {
-
- foreach ($event->getEntity()->getProductFamilySectionProperties() as $productFamilySectionProperty) {
- $this->setProductCategoryByProductFamilySectionProperty($productFamilySectionProperty);
- }
-
-
- }
- }
-
- protected function setProductCategoryByProductFamilySectionProperty($productFamilySectionProperty)
- {
- $productFamily = $productFamilySectionProperty->getProductFamily();
-
- if ($productFamilySectionProperty->getStatus() == 1 && $productFamily->getStatus() == 1) {
- $section = $productFamilySectionProperty->getSection();
- $productCategoryArray = $productFamilySectionProperty->getProductFamily()->getProductCategories();
- foreach ($productCategoryArray as $productCategory) {
- if ($productCategory->getSection() === $section) {
- $this->productCategoryContainer->getBuilder()->setOnlineIfOnlineProductfamily($productCategory);
- }
- }
- } else if ($productFamilySectionProperty->getStatus() == 0 || $productFamily->getStatus() == 0) {
- $section = $productFamilySectionProperty->getSection();
- $productCategoryArray = $productFamilySectionProperty->getProductFamily()->getProductCategories();
- foreach ($productCategoryArray as $productCategory) {
- if ($productCategory->getSection() === $section) {
- $this->productCategoryContainer->getBuilder()->setOfflineIfOfflineProductfamily($productCategory);
- }
- }
-
- }
- }
- }
|