<commit_before><?php
namespace Radar\Adr;

use Exception as AnyException;
use Aura\Di\Injection\InjectionFactory;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class Dispatcher implements DispatcherInterface
{
    protected $factory;
    protected $request;
    protected $response;

    public function __construct(
        InjectionFactory $factory,
        ServerRequestInterface $request,
        ResponseInterface $response
    ) {
        $this->factory = $factory;
        $this->request = $request;
        $this->response = $response;
    }

    public function __invoke(
        array $middle,
        $routingHandler,
        $sendingHandler,
        $exceptionHandler
    ) {
        try {
            $this->inbound($middle, $routingHandler);
        } catch (AnyException $e) {
            $this->handleException($e, $exceptionHandler);
        }

        try {
            $this->outbound($middle, $sendingHandler);
        } catch (AnyException $e) {
            $this->handleException($e, $exceptionHandler);
        }
    }

    public function inbound($middle, $routingHandler)
    {
        $early = $this->middle($middle, 'before');
        if ($early) {
            return;
        }

        $routingHandler = $this->factory($routingHandler);
        $route = $routingHandler($this->request);
        $this->action($route);
        $this->middle($middle, 'after');
    }

    protected function outbound($middle, $sendingHandler)
    {
        $sendingHandler = $this->factory($sendingHandler);
        $sendingHandler($this->response);
        $this->middle($middle, 'finish');
    }

    protected function middle(array $middle, $key)
    {
        foreach ($middle[$key] as $class) {
            $object = $this->factory($class);
            $early = $object($this->request, $this->response);
            if ($early instanceof ResponseInterface) {
                $this->response = $early;
                return true;
            }
        }
        return false;
    }

    protected function action($route)
    {
        foreach ($route->attributes as $key => $val) {
            $this->request = $this->request->withAttribute($key, $val);
        }
        $responder = $this->factory($route->responder);
        $this->response = $route->domain
            ? $responder($this->request, $this->response, $this->payload($route->input, $route->domain))
            : $responder($this->request, $this->response);
    }

    protected function payload($input, $domain)
    {
        if ($input) {
            $input = $this->factory($input);
            $input = (array) $input($this->request);
        } else {
            $input = [];
        }

        $domain = $this->factory($domain);
        return call_user_func_array($domain, $input);
    }

    protected function handleException($exception, $handler)<commit_msg>minor refactoring<commit_after><?php
namespace Radar\Adr;

use Exception as AnyException;
use Aura\Di\Injection\InjectionFactory;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class Dispatcher implements DispatcherInterface
{
    protected $factory;
    protected $request;
    protected $response;

    public function __construct(
        InjectionFactory $factory,
        ServerRequestInterface $request,
        ResponseInterface $response
    ) {
        $this->factory = $factory;
        $this->request = $request;
        $this->response = $response;
    }

    public function __invoke(
        array $middle,
        $routingHandler,
        $sendingHandler,
        $exceptionHandler
    ) {
        try {
            $this->inbound($middle, $routingHandler);
        } catch (AnyException $e) {
            $this->handleException($e, $exceptionHandler);
        }

        try {
            $this->outbound($middle, $sendingHandler);
        } catch (AnyException $e) {
            $this->handleException($e, $exceptionHandler);
        }
    }

    public function inbound($middle, $routingHandler)
    {
        $early = $this->middle($middle, 'before');
        if ($early) {
            return;
        }

        $routingHandler = $this->factory($routingHandler);
        $route = $routingHandler($this->request);
        $this->response = $this->action($route);
        $this->middle($middle, 'after');
    }

    protected function outbound($middle, $sendingHandler)
    {
        $sendingHandler = $this->factory($sendingHandler);
        $sendingHandler($this->response);
        $this->middle($middle, 'finish');
    }

    protected function middle(array $middle, $key)
    {
        foreach ($middle[$key] as $class) {
            $object = $this->factory($class);
            $early = $object($this->request, $this->response);
            if ($early instanceof ResponseInterface) {
                $this->response = $early;
                return true;
            }
        }
        return false;
    }

    protected function action($route)
    {
        foreach ($route->attributes as $key => $val) {
            $this->request = $this->request->withAttribute($key, $val);
        }

        $responder = $this->factory($route->responder);

        if ($route->domain) {
            return $responder(
                $this->request,
                $this->response,
                $this->payload($route->input, $route->domain)
            );
        }

        return $responder($this->request, $this->response);
    }

    protected function payload($input, $domain)
    {
        if ($input) {
            $input = $this->factory($input);
            $input = (array) $input($this->request);
        } else {
            $input = [];
        }

        $domain = $this->factory($domain);
        return call_user_func_array($domain, $input);
    }

    protected function handleException($exception, $handler)
{'fraction_non_alphanumeric': 0.13774902094329985, 'fraction_numerical': 0.0, 'mean_word_length': 2.175135135135135, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, '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': '1956113', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1576, 'n_words': 314}