<commit_before><?php

namespace Victoire\Bundle\CoreBundle\EventSubscriber;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\UnitOfWork;
use Victoire\Bundle\CoreBundle\Builder\ViewCssBuilder;
use Victoire\Bundle\CoreBundle\Entity\View;
use Victoire\Bundle\CoreBundle\Repository\ViewRepository;
use Victoire\Bundle\TemplateBundle\Entity\Template;
use Victoire\Bundle\WidgetBundle\Entity\Widget;
use Victoire\Bundle\WidgetBundle\Repository\WidgetRepository;
use Victoire\Bundle\WidgetMapBundle\Builder\WidgetMapBuilder;

class WidgetSubscriber implements EventSubscriber
{
    private $viewCssBuilder;
    private $widgetMapBuilder;
    /* @var UnitOfWork $uow */
    private $uow;
    /* @var EntityManager $em */
    private $em;
    /* @var WidgetRepository $widgetRepo */
    private $widgetRepo;
    /* @var ViewRepository $viewRepo */
    private $viewRepo;

    /**
     * Construct.
     *
     * @param ViewCssBuilder $viewCssBuilder
     */
    public function __construct(ViewCssBuilder $viewCssBuilder, WidgetMapBuilder $widgetMapBuilder)
    {
        $this->viewCssBuilder = $viewCssBuilder;
        $this->widgetMapBuilder = $widgetMapBuilder;
    }

    /**
     * Get SubscribedEvents.
     *
     * @return array
     */
    public function getSubscribedEvents()
    {
        return [
            'onFlush',
        ];
    }

    /**
     * Change cssHash of views when a widget is updated or deleted.
     *
     * @param OnFlushEventArgs $args
     */
    public function onFlush(OnFlushEventArgs $args)
    {
        $this->em = $args->getEntityManager();
        $this->uow = $this->em->getUnitOfWork();
        $this->widgetRepo = $this->em->getRepository('Victoire\Bundle\WidgetBundle\Entity\Widget');
        $this->viewRepo = $this->em->getRepository('Victoire\Bundle\CoreBundle\Entity\View');

        $updatedEntities = $this->uow->getScheduledEntityUpdates();
        $deletedEntities = $this->uow->getScheduledEntityDeletions();
        //Update View's CSS and inheritors of updated and deleted widgets
        foreach (array_merge($updatedEntities) as $entity) {
            if (!($entity instanceof Widget)) {
                continue;
            }

            /** @var Widget $entity */
            foreach ($entity->getWidgetMaps() as $widgetMap) {
                $view = $widgetMap->getView();
                $this->updateViewCss($view);
                $this->updateTemplateInheritorsCss($view);
            }
        }

        //Remove CSS of deleted View and update its inheritors
        foreach ($deletedEntities as $entity) {<commit_msg>re-compute the view css only when the persisted widget mode is not delete<commit_after><?php

namespace Victoire\Bundle\CoreBundle\EventSubscriber;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\UnitOfWork;
use Victoire\Bundle\CoreBundle\Builder\ViewCssBuilder;
use Victoire\Bundle\CoreBundle\Entity\View;
use Victoire\Bundle\CoreBundle\Repository\ViewRepository;
use Victoire\Bundle\TemplateBundle\Entity\Template;
use Victoire\Bundle\WidgetBundle\Entity\Widget;
use Victoire\Bundle\WidgetBundle\Repository\WidgetRepository;
use Victoire\Bundle\WidgetMapBundle\Builder\WidgetMapBuilder;
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;

class WidgetSubscriber implements EventSubscriber
{
    private $viewCssBuilder;
    private $widgetMapBuilder;
    /* @var UnitOfWork $uow */
    private $uow;
    /* @var EntityManager $em */
    private $em;
    /* @var WidgetRepository $widgetRepo */
    private $widgetRepo;
    /* @var ViewRepository $viewRepo */
    private $viewRepo;

    /**
     * Construct.
     *
     * @param ViewCssBuilder $viewCssBuilder
     */
    public function __construct(ViewCssBuilder $viewCssBuilder, WidgetMapBuilder $widgetMapBuilder)
    {
        $this->viewCssBuilder = $viewCssBuilder;
        $this->widgetMapBuilder = $widgetMapBuilder;
    }

    /**
     * Get SubscribedEvents.
     *
     * @return array
     */
    public function getSubscribedEvents()
    {
        return [
            'onFlush',
        ];
    }

    /**
     * Change cssHash of views when a widget is updated or deleted.
     *
     * @param OnFlushEventArgs $args
     */
    public function onFlush(OnFlushEventArgs $args)
    {
        $this->em = $args->getEntityManager();
        $this->uow = $this->em->getUnitOfWork();
        $this->widgetRepo = $this->em->getRepository('Victoire\Bundle\WidgetBundle\Entity\Widget');
        $this->viewRepo = $this->em->getRepository('Victoire\Bundle\CoreBundle\Entity\View');

        $updatedEntities = $this->uow->getScheduledEntityUpdates();
        $deletedEntities = $this->uow->getScheduledEntityDeletions();
        //Update View's CSS and inheritors of updated and deleted widgets
        foreach (array_merge($updatedEntities) as $entity) {
            if (!($entity instanceof Widget)) {
                continue;
            }

            /** @var Widget $entity */
            foreach ($entity->getWidgetMaps() as $widgetMap) {
                if ($widgetMap->getAction() !== WidgetMap::ACTION_DELETE) {
                    $view = $widgetMap->getView();
                    $this->updateViewCss($view);
                    $this->updateTemplateInheritorsCss($view);
                }
            }
        }

        //Remove CSS of deleted View and update its inheritors
        foreach ($deletedEntities as $entity) {
{'fraction_non_alphanumeric': 0.11295561169252977, 'fraction_numerical': 0.0, 'mean_word_length': 2.7784594410361283, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5931994', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1579, 'n_words': 315}