<commit_before><?php

namespace Tienvx\Bundle\MbtBundle\EventListener;

use Doctrine\Common\Annotations\Reader;
use Exception;
use ReflectionMethod;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent;
use Tienvx\Bundle\MbtBundle\Annotation\DataProvider;
use Tienvx\Bundle\MbtBundle\Subject\AbstractSubject;

class WorkflowSubscriber implements EventSubscriberInterface
{
    /** @var Reader $reader */
    protected $reader;

    public function __construct(Reader $reader)
    {
        $this->reader = $reader;
    }

    public function onEntered(Event $event)
    {
        $subject = $event->getSubject();

        if ($subject instanceof AbstractSubject) {
            $places = array_keys(array_filter($event->getMarking()->getPlaces()));
            $subject->enterPlace($places);
        }
    }

    public function onTransition(Event $event)
    {
        $subject = $event->getSubject();

        if ($subject instanceof AbstractSubject) {
            $subject->storeData();
            $subject->applyTransition($event->getTransition()->getName());
        }
    }

    /**
     * @param GuardEvent $event
     *
     * @throws Exception
     */
    public function onGuard(GuardEvent $event)
    {
        $subject = $event->getSubject();
        $transitionName = $event->getTransition()->getName();

        if ($subject instanceof AbstractSubject) {
            if ($subject->needData() && method_exists($subject, $transitionName)) {
                $reflectionMethod = new ReflectionMethod(get_class($subject), $transitionName);
                $dataProvider = $this->reader->getMethodAnnotation($reflectionMethod, DataProvider::class);
                if ($dataProvider && $dataProvider instanceof DataProvider) {
                    $data = call_user_func([$subject, $dataProvider->method]);
                    if (!is_array($data)) {
                        return;
                    }
                } else {
                    $data = [];
                }
                $subject->setData($data);
            }
        }
    }

    public static function getSubscribedEvents()
    {
        return [
            'workflow.guard' => 'onGuard',
            'workflow.transition' => 'onTransition',
            'workflow.entered' => 'onEntered',
        ];
    }
}<commit_msg>Use new event subscriber syntax<commit_after><?php

namespace Tienvx\Bundle\MbtBundle\EventListener;

use Doctrine\Common\Annotations\Reader;
use Exception;
use ReflectionMethod;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\EnteredEvent;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Event\TransitionEvent;
use Tienvx\Bundle\MbtBundle\Annotation\DataProvider;
use Tienvx\Bundle\MbtBundle\Subject\AbstractSubject;

class WorkflowSubscriber implements EventSubscriberInterface
{
    /** @var Reader $reader */
    protected $reader;

    public function __construct(Reader $reader)
    {
        $this->reader = $reader;
    }

    public function onEntered(EnteredEvent $event)
    {
        $subject = $event->getSubject();

        if ($subject instanceof AbstractSubject) {
            $places = array_keys(array_filter($event->getMarking()->getPlaces()));
            $subject->enterPlace($places);
        }
    }

    public function onTransition(TransitionEvent $event)
    {
        $subject = $event->getSubject();

        if ($subject instanceof AbstractSubject) {
            $subject->storeData();
            $subject->applyTransition($event->getTransition()->getName());
        }
    }

    /**
     * @param GuardEvent $event
     *
     * @throws Exception
     */
    public function onGuard(GuardEvent $event)
    {
        $subject = $event->getSubject();
        $transitionName = $event->getTransition()->getName();

        if ($subject instanceof AbstractSubject) {
            if ($subject->needData() && method_exists($subject, $transitionName)) {
                $reflectionMethod = new ReflectionMethod(get_class($subject), $transitionName);
                $dataProvider = $this->reader->getMethodAnnotation($reflectionMethod, DataProvider::class);
                if ($dataProvider && $dataProvider instanceof DataProvider) {
                    $data = call_user_func([$subject, $dataProvider->method]);
                    if (!is_array($data)) {
                        return;
                    }
                } else {
                    $data = [];
                }
                $subject->setData($data);
            }
        }
    }

    public static function getSubscribedEvents()
    {
        return [
            GuardEvent::class => 'onGuard',
            TransitionEvent::class => 'onTransition',
            EnteredEvent::class => 'onEntered',
        ];
    }
}
{'fraction_non_alphanumeric': 0.12085452695829095, 'fraction_numerical': 0.0, 'mean_word_length': 2.3625170998632012, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '2876333', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1244, 'n_words': 230}