<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2017, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\MenuBundle\EventListener;

use Darvin\ContentBundle\Entity\SlugMapItem;
use Darvin\ContentBundle\Translatable\TranslationsInitializerInterface;
use Darvin\MenuBundle\Entity\Menu\Item;
use Darvin\MenuBundle\Item\MenuItemManager;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Events;

/**
 * Manage menu items event subscriber
 */
class ManageItemsSubscriber implements EventSubscriber
{
    /**
     * @var \Darvin\MenuBundle\Item\MenuItemManager
     */
    private $menuItemManager;

    /**
     * @var \Darvin\ContentBundle\Translatable\TranslationsInitializerInterface
     */
    private $translationInitializer;

    /**
     * @var string[]
     */
    private $locales;

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $em;

    /**
     * @param \Darvin\MenuBundle\Item\MenuItemManager                             $menuItemManager         Menu item manager
     * @param \Darvin\ContentBundle\Translatable\TranslationsInitializerInterface $translationsInitializer Translation initializer
     * @param string[]                                                            $locales                 Locales
     */
    public function __construct(MenuItemManager $menuItemManager, TranslationsInitializerInterface $translationsInitializer, array $locales)
    {
        $this->menuItemManager = $menuItemManager;
        $this->translationInitializer = $translationsInitializer;
        $this->locales = $locales;

        $this->em = null;
    }

    /**
     * {@inheritdoc}
     */
    public function getSubscribedEvents()
    {
        return [
            Events::preFlush,
        ];
    }

    /**
     * @param \Doctrine\ORM\Event\PreFlushEventArgs $args Event arguments
     */
    public function preFlush(PreFlushEventArgs $args)
    {
        $this->em = $em = $args->getEntityManager();

        foreach ($this->menuItemManager->getScheduledForAdding() as $menuAlias => $entities) {
            foreach ($entities as $entity) {
                $slugMapItem = $this->getSlugMapItem($entity);

                if (empty($slugMapItem)) {
                    continue;
                }

                $em->persist($this->createMenuItem($menuAlias, $slugMapItem));
            }
        }
    }

    /**<commit_msg>Remove menu items in the manage menu items event subscriber.<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2017, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\MenuBundle\EventListener;

use Darvin\ContentBundle\Entity\SlugMapItem;
use Darvin\ContentBundle\Translatable\TranslationsInitializerInterface;
use Darvin\MenuBundle\Entity\Menu\Item;
use Darvin\MenuBundle\Item\MenuItemManager;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Events;

/**
 * Manage menu items event subscriber
 */
class ManageItemsSubscriber implements EventSubscriber
{
    /**
     * @var \Darvin\MenuBundle\Item\MenuItemManager
     */
    private $menuItemManager;

    /**
     * @var \Darvin\ContentBundle\Translatable\TranslationsInitializerInterface
     */
    private $translationInitializer;

    /**
     * @var string[]
     */
    private $locales;

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $em;

    /**
     * @param \Darvin\MenuBundle\Item\MenuItemManager                             $menuItemManager         Menu item manager
     * @param \Darvin\ContentBundle\Translatable\TranslationsInitializerInterface $translationsInitializer Translation initializer
     * @param string[]                                                            $locales                 Locales
     */
    public function __construct(MenuItemManager $menuItemManager, TranslationsInitializerInterface $translationsInitializer, array $locales)
    {
        $this->menuItemManager = $menuItemManager;
        $this->translationInitializer = $translationsInitializer;
        $this->locales = $locales;

        $this->em = null;
    }

    /**
     * {@inheritdoc}
     */
    public function getSubscribedEvents()
    {
        return [
            Events::preFlush,
        ];
    }

    /**
     * @param \Doctrine\ORM\Event\PreFlushEventArgs $args Event arguments
     */
    public function preFlush(PreFlushEventArgs $args)
    {
        $this->em = $em = $args->getEntityManager();

        foreach ($this->menuItemManager->getScheduledForAdding() as $menuAlias => $entities) {
            foreach ($entities as $entity) {
                $slugMapItem = $this->getSlugMapItem($entity);

                if (empty($slugMapItem)) {
                    continue;
                }

                $em->persist($this->createMenuItem($menuAlias, $slugMapItem));
            }
        }
        foreach ($this->menuItemManager->getScheduledForRemoval() as $menuAlias => $entities) {
            foreach ($entities as $entity) {
                $slugMapItem = $this->getSlugMapItem($entity);

                if (empty($slugMapItem)) {
                    continue;
                }

                $menuItems = $em->getRepository(Item::class)->findBy([
                    'menu'        => $menuAlias,
                    'slugMapItem' => $slugMapItem,
                ]);

                foreach ($menuItems as $menuItem) {
                    $em->remove($menuItem);
                }
            }
        }
    }

    /**
{'fraction_non_alphanumeric': 0.1092436974789916, 'fraction_numerical': 0.0013445378151260505, 'mean_word_length': 2.209816612729234, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 42, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5885897', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1590, 'n_words': 312}