<commit_before><?php

namespace ClassEvent\Event\Base;

use ClassEvent\Event\Base\Interfaces\EventManagerInterface;
use ClassEvent\Event\Base\Interfaces\EventInterface;
use Zend\Config\Reader;

class EventManager implements EventManagerInterface
{
    /**
     * store all loaded configuration
     *
     * @var array
     */
    protected $_eventsConfig = [];

    /**
     * store all called events
     *
     * @var array
     */
    protected $_events = [];

    /**
     * 
     *
     * @var bool
     */
    protected $_hasErrors = false;

    /**
     * store all errors
     *
     * @var
     */
    protected $_errorList = [];

    /**
     * store default options for event manager
     *
     * @var array
     */
    protected $_options = [
        'events'            => [],
        'type'              => 'array',
        'log_events'        => false,
        'from_file'         => false
    ];

    /**
     * create manage instance
     *
     * @param array $options
     */
    public function __construct(array $options = [])
    {
        $config = array_merge($this->_options, $options);

        if ($config['from_file']) {
            $this->_eventsConfig = $this->readEventConfiguration(
                $config['events'],
                $config['type']
            );
        } else {
            $this->_eventsConfig = array_merge(
                $this->_eventsConfig,
                $config['events']
            );
        }
    }

    /**
     * return event object or create it if not exist
     *
     * @param string $eventName
     * @return EventInterface
     */
    public function getEventObject($eventName)
    {
        if (!array_key_exists($eventName, $this->_eventsConfig)) {
            throw new \InvalidArgumentException('Event is not defined.');
        }

        if (!array_key_exists($eventName, $this->_events)) {
            $namespace                  = $this->_eventsConfig[$eventName]['object'];
            $instance                   = new $namespace;

            if (!($instance instanceof EventInterface)) {
                throw new \LogicException('Invalid interface of event object');
            }

            $this->_events[$eventName] = $instance;
        }

        return $this->_events[$eventName];
    }

    /**
     * add event configuration into event manager
     *
     * @param array $config
     * @return $this
     */
    public function setEventConfiguration(array $config)
    {
        $config = array_merge($this->_options, $config);

        if ($config['from_file']) {
            $configuration = $this->readEventConfiguration(
                $config['events'],
                $config['type']
            );
        } else {
            $configuration = $config['events'];
        }

        $this->_eventsConfig = array_merge_recursive(
            $this->_eventsConfig,
            $configuration
        );

        return $this;
    }

    /**
     * trigger new event with automatic call all subscribed listeners
     *
     * @param string $name
     * @param array $data
     * @return $this<commit_msg>changed configuration setup<commit_after><?php

namespace ClassEvent\Event\Base;

use ClassEvent\Event\Base\Interfaces\EventManagerInterface;
use ClassEvent\Event\Base\Interfaces\EventInterface;
use Zend\Config\Reader;

class EventManager implements EventManagerInterface
{
    /**
     * store all loaded configuration
     *
     * @var array
     */
    protected $_eventsConfig = [];

    /**
     * store all called events
     *
     * @var array
     */
    protected $_events = [];

    /**
     * 
     *
     * @var bool
     */
    protected $_hasErrors = false;

    /**
     * store all errors
     *
     * @var
     */
    protected $_errorList = [];

    /**
     * store default options for event manager
     *
     * @var array
     */
    protected $_options = [
        'events'            => [],
        'type'              => 'array',
        'log_events'        => false,
        'from_file'         => false
    ];

    /**
     * create manage instance
     *
     * @param array $options
     */
    public function __construct(array $options = [])
    {
        $this->_options = array_merge($this->_options, $options);

        if ($this->_options['from_file']) {
            $this->_eventsConfig = $this->readEventConfiguration(
                $this->_options['events'],
                $this->_options['type']
            );
        } else {
            $this->_eventsConfig = array_merge(
                $this->_eventsConfig,
                $this->_options['events']
            );
        }

        unset($this->_options['events']);
    }

    /**
     * return event object or create it if not exist
     *
     * @param string $eventName
     * @return EventInterface
     */
    public function getEventObject($eventName)
    {
        if (!array_key_exists($eventName, $this->_eventsConfig)) {
            throw new \InvalidArgumentException('Event is not defined.');
        }

        if (!array_key_exists($eventName, $this->_events)) {
            $namespace                  = $this->_eventsConfig[$eventName]['object'];
            $instance                   = new $namespace;

            if (!($instance instanceof EventInterface)) {
                throw new \LogicException('Invalid interface of event object');
            }

            $this->_events[$eventName] = $instance;
        }

        return $this->_events[$eventName];
    }

    /**
     * add event configuration into event manager
     *
     * @param array $config
     * @return $this
     */
    public function setEventConfiguration(array $config)
    {
        $this->_options = array_merge($this->_options, $config);

        if ($this->_options['from_file']) {
            $configuration = $this->readEventConfiguration(
                $this->_options['events'],
                $this->_options['type']
            );
        } else {
            $configuration = $this->_options['events'];
        }

        $this->_eventsConfig = array_merge_recursive(
            $this->_eventsConfig,
            $configuration
        );

        unset($this->_options['events']);
        return $this;
    }

    /**
     * trigger new event with automatic call all subscribed listeners
     *
     * @param string $name
     * @param array $data
     * @return $this
{'fraction_non_alphanumeric': 0.12455014864653419, 'fraction_numerical': 0.0, 'mean_word_length': 1.710771840542833, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, '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': '1533030', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1632, 'n_words': 363}