<commit_before><?php
/*
 * This file is part of the prooph/service-bus.
 * (c) 2014-2015 prooph software GmbH <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * Date: 05/23/15 - 6:22 PM
 */

namespace Prooph\ServiceBus\Plugin\Router;

use Assert\Assertion;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\Common\Event\ActionEventListenerAggregate;
use Prooph\Common\Event\DetachAggregateHandlers;
use Prooph\ServiceBus\Exception\RuntimeException;
use Prooph\ServiceBus\MessageBus;

/**
 * Class SingleHandlerRouter
 *
 * @package Prooph\ServiceBus\Router
 * @author <NAME> <<EMAIL>>
 */
class SingleHandlerRouter implements ActionEventListenerAggregate
{
    use DetachAggregateHandlers;

    /**
     * @var array[messageName => messageHandler]
     */
    protected $messageMap = [];

    /**
     * @var string
     */
    protected $tmpMessageName;

    /**
     * @param null|array[messageName => messageHandler] $commandMap
     */
    public function __construct(array $messageMap = null)
    {
        if (! is_null($messageMap)) {
            foreach ($messageMap as $messageName => $handler) {
                $this->route($messageName)->to($handler);
            }
        }
    }

    /**
     * @param ActionEventEmitter $events
     *
     * @return void
     */
    public function attach(ActionEventEmitter $events)
    {
        $this->trackHandler($events->attachListener(MessageBus::EVENT_ROUTE, [$this, "onRouteMessage"]));
    }

    /**
     * @param string $messageName
     * @return $this
     * @throws \Prooph\ServiceBus\Exception\RuntimeException
     */
    public function route($messageName)
    {
        Assertion::string($messageName);
        Assertion::notEmpty($messageName);

        if (! is_null($this->tmpMessageName)) {
            throw new RuntimeException(sprintf("Message %s is not mapped to a handler.", $this->tmpMessageName));
        }

        $this->tmpMessageName = $messageName;

        return $this;
    }

    /**
     * @param string|object|callable $messageHandler
     * @return $this
     * @throws \Prooph\ServiceBus\Exception\RuntimeException
     * @throws \InvalidArgumentException
     */
    public function to($messageHandler)
    {
        if (! is_string($messageHandler) && ! is_object($messageHandler) && ! is_callable($messageHandler)) {
            throw new \InvalidArgumentException(sprintf(
                "Invalid message handler provided. Expected type is string, object or callable but type of %s given.",
                gettype($messageHandler)
            ));
        }

        if (is_null($this->tmpMessageName)) {
            throw new RuntimeException(sprintf(
                "Cannot map handler %s to a message. Please use method route before calling method to",
                (is_object($messageHandler))? get_class($messageHandler) : (is_string($messageHandler))? $messageHandler : gettype($messageHandler)
            ));
        }

        $this->messageMap[$this->tmpMessageName] = $messageHandler;

        $this->tmpMessageName = null;

        return $this;
    }

    /**
     * @param ActionEvent $actionEvent
     */<commit_msg>use custom invalid argument exception<commit_after><?php
/*
 * This file is part of the prooph/service-bus.
 * (c) 2014-2015 prooph software GmbH <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * Date: 05/23/15 - 6:22 PM
 */

namespace Prooph\ServiceBus\Plugin\Router;

use Assert\Assertion;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\Common\Event\ActionEventListenerAggregate;
use Prooph\Common\Event\DetachAggregateHandlers;
use Prooph\ServiceBus\Exception;
use Prooph\ServiceBus\MessageBus;

/**
 * Class SingleHandlerRouter
 *
 * @package Prooph\ServiceBus\Router
 * @author <NAME> <<EMAIL>>
 */
class SingleHandlerRouter implements ActionEventListenerAggregate
{
    use DetachAggregateHandlers;

    /**
     * @var array[messageName => messageHandler]
     */
    protected $messageMap = [];

    /**
     * @var string
     */
    protected $tmpMessageName;

    /**
     * @param null|array[messageName => messageHandler] $commandMap
     */
    public function __construct(array $messageMap = null)
    {
        if (! is_null($messageMap)) {
            foreach ($messageMap as $messageName => $handler) {
                $this->route($messageName)->to($handler);
            }
        }
    }

    /**
     * @param ActionEventEmitter $events
     *
     * @return void
     */
    public function attach(ActionEventEmitter $events)
    {
        $this->trackHandler($events->attachListener(MessageBus::EVENT_ROUTE, [$this, "onRouteMessage"]));
    }

    /**
     * @param string $messageName
     * @return $this
     * @throws Exception\RuntimeException
     */
    public function route($messageName)
    {
        Assertion::string($messageName);
        Assertion::notEmpty($messageName);

        if (! is_null($this->tmpMessageName)) {
            throw new Exception\RuntimeException(sprintf("Message %s is not mapped to a handler.", $this->tmpMessageName));
        }

        $this->tmpMessageName = $messageName;

        return $this;
    }

    /**
     * @param string|object|callable $messageHandler
     * @return $this
     * @throws Exception\RuntimeException
     * @throws Exception\InvalidArgumentException
     */
    public function to($messageHandler)
    {
        if (! is_string($messageHandler) && ! is_object($messageHandler) && ! is_callable($messageHandler)) {
            throw new Exception\InvalidArgumentException(sprintf(
                "Invalid message handler provided. Expected type is string, object or callable but type of %s given.",
                gettype($messageHandler)
            ));
        }

        if (is_null($this->tmpMessageName)) {
            throw new Exception\RuntimeException(sprintf(
                "Cannot map handler %s to a message. Please use method route before calling method to",
                (is_object($messageHandler))? get_class($messageHandler) : (is_string($messageHandler))? $messageHandler : gettype($messageHandler)
            ));
        }

        $this->messageMap[$this->tmpMessageName] = $messageHandler;

        $this->tmpMessageName = null;

        return $this;
    }

    /**
     * @param ActionEvent $actionEvent
     */
{'fraction_non_alphanumeric': 0.11715289445547579, 'fraction_numerical': 0.005193218267908966, 'mean_word_length': 2.745995423340961, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 41, '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': '3213109', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1771, 'n_words': 412}