<commit_before><?php

/**
 * This file belongs to Kreta.
 * The source code of application includes a LICENSE file
 * with all information about license.
 *
 * @author benatespina <<EMAIL>>
 * @author gorkalaucirica <<EMAIL>>
 */

namespace Kreta\Component\Notification\EventSubscriber;

use Doctrine\Common\Persistence\ObjectManager;
use Kreta\Component\Notification\NotifiableEvent\Registry\NotifiableEventRegistryInterface;
use Kreta\Component\Notification\Notifier\Registry\NotifierRegistryInterface;

/**
 * Class AbstractEventSubscriber
 *
 * @package Kreta\Component\Notification\EventSubscriber
 */
abstract class AbstractEventSubscriber
{
    protected $notifiableEventRegistry;

    protected $notifierRegistry;

    /**
     * Builds the subscriber with the required dependencies. Both will be used in handleEvent method to notify users
     *
     * @param NotifiableEventRegistryInterface $notifiableEventRegistry Registry containing all events to be notified
     * @param NotifierRegistryInterface        $notifierRegistry        Registry containing all notifiers enabled
     */
    public function __construct(NotifiableEventRegistryInterface $notifiableEventRegistry,
                                NotifierRegistryInterface $notifierRegistry)
    {
        $this->notifiableEventRegistry = $notifiableEventRegistry;
        $this->notifierRegistry = $notifierRegistry;
    }

    /**
     * Returns an array of events this subscriber wants to listen to.
     *
     * @return array
     */
    abstract public function getSubscribedEvents();

    /**
     * Handles the event for the object passed as parameter. It calls all subscribed notifiable events that will manage
     * the event and will call the registered notifiers to send notifications to users. Additionally, persists all
     * notification in database
     *
     * @param string                                     $event   Event that was triggered
     * @param object                                     $object  Object that triggered the event
     * @param \Doctrine\Common\Persistence\ObjectManager $manager Entity manager used to persist built in notification
     */
    public function handleEvent($event, $object, ObjectManager $manager = null)
    {
        $notifications = array();

        foreach ($this->notifiableEventRegistry->getNotifiableEvents() as $notifiable) {
            if ($notifiable->supportsEvent($event, $object)) {
                $notifications = array_merge($notifications, $notifiable->getNotifications($event, $object));
            }
        }

        foreach ($notifications as $notification) {
            foreach ($this->notifierRegistry->getNotifiers() as $notifier) {
                $notifier->notify($notification);
            }
            if ($manager) {
                $manager->persist($notification);
            }
        }

        if($manager) {
            $manager->flush();
        }
    }
}<commit_msg>Fixed issues with event subscriber<commit_after><?php

/**
 * This file belongs to Kreta.
 * The source code of application includes a LICENSE file
 * with all information about license.
 *
 * @author benatespina <<EMAIL>>
 * @author gorkalaucirica <<EMAIL>>
 */

namespace Kreta\Component\Notification\EventSubscriber;

use Doctrine\Common\Persistence\ObjectManager;
use Kreta\Component\Notification\NotifiableEvent\Registry\NotifiableEventRegistryInterface;
use Kreta\Component\Notification\Notifier\Registry\NotifierRegistryInterface;

/**
 * Class AbstractEventSubscriber
 *
 * @package Kreta\Component\Notification\EventSubscriber
 */
abstract class AbstractEventSubscriber
{
    protected $notifiableEventRegistry;

    protected $notifierRegistry;

    /**
     * Builds the subscriber with the required dependencies. Both will be used in handleEvent method to notify users
     *
     * @param NotifiableEventRegistryInterface $notifiableEventRegistry Registry containing all events to be notified
     * @param NotifierRegistryInterface        $notifierRegistry        Registry containing all notifiers enabled
     */
    public function __construct(NotifiableEventRegistryInterface $notifiableEventRegistry,
                                NotifierRegistryInterface $notifierRegistry)
    {
        $this->notifiableEventRegistry = $notifiableEventRegistry;
        $this->notifierRegistry = $notifierRegistry;
    }

    /**
     * Returns an array of events this subscriber wants to listen to.
     *
     * @return array
     */
    abstract public function getSubscribedEvents();

    /**
     * Handles the event for the object passed as parameter. It calls all subscribed notifiable events that will manage
     * the event and will call the registered notifiers to send notifications to users. Additionally, persists all
     * notification in database
     *
     * @param string                                     $event   Event that was triggered
     * @param object                                     $object  Object that triggered the event
     * @param \Doctrine\Common\Persistence\ObjectManager $manager Entity manager used to persist built in notification
     */
    public function handleEvent($event, $object, ObjectManager $manager = null)
    {
        $notifications = array();

        foreach ($this->notifiableEventRegistry->getNotifiableEvents() as $notifiable) {
            if ($notifiable->supportsEvent($event, $object)) {
                $notifications = array_merge($notifications, $notifiable->getNotifications($event, $object));
            }
        }

        foreach ($notifications as $notification) {
            foreach ($this->notifierRegistry->getNotifiers() as $notifier) {
                $notifier->notify($notification);
            }
            if ($manager) {
                $manager->persist($notification);
            }
        }

        if(count($notifications) > 0 && $manager) {
            $manager->flush();
        }
    }
}
{'fraction_non_alphanumeric': 0.08139926000672722, 'fraction_numerical': 0.00016818028927009755, 'mean_word_length': 2.7473219911783238, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '536072', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1378, 'n_words': 414}