<commit_before><?php
namespace HttpVerbExtraction\Rest;

use ZF\ApiProblem\ApiProblem;
use ZF\Rest\ResourceEvent;
use HttpVerbExtraction\Service\VerbServiceName;
use HttpVerbExtraction\ErrorMessage\ErrorMessage;
use HttpVerbExtraction\DispatchableInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use HttpVerbExtraction\Exception\ServiceNotFound;

class DispatchVerb implements ServiceLocatorAwareInterface {

    private $errorMessage;

    private $verbServiceName;

    private $serviceManager;

    public function __construct(ErrorMessage $errorMessage, VerbServiceName $verbServiceName)
    {
        $this->errorMessage    = $errorMessage;
        $this->verbServiceName = $verbServiceName;
    }


    public function dispatch(ResourceEvent $event)
    {
        $serviceName  = $this->getServiceName($event);

        $service = $this->getServiceInstance($serviceName);

        if($service instanceof DispatchableInterface)
            return $service->dispatch($event);

        return $this->getErrorMessage($event);
    }

    private function getErrorMessage(ResourceEvent $event)
    {
        $message = $this->errorMessage->get($event);
        return new ApiProblem(405, $message);<commit_msg>Catching exceptions and returning proper error message<commit_after><?php
namespace HttpVerbExtraction\Rest;

use ZF\ApiProblem\ApiProblem;
use ZF\Rest\ResourceEvent;
use HttpVerbExtraction\Service\VerbServiceName;
use HttpVerbExtraction\ErrorMessage\ErrorMessage;
use HttpVerbExtraction\DispatchableInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use HttpVerbExtraction\Exception\ServiceNotFound;
use HttpVerbExtraction\Exception\ServiceNameNotFound;

class DispatchVerb implements ServiceLocatorAwareInterface {

    private $errorMessage;

    private $verbServiceName;

    private $serviceManager;

    public function __construct(ErrorMessage $errorMessage, VerbServiceName $verbServiceName)
    {
        $this->errorMessage    = $errorMessage;
        $this->verbServiceName = $verbServiceName;
    }


    public function dispatch(ResourceEvent $event)
    {
        try{
            $serviceName  = $this->getServiceName($event);

            $service = $this->getServiceInstance($serviceName);

            if($service instanceof DispatchableInterface)
                return $service->dispatch($event);
        }
        Catch(ServiceNameNotFound $e) {
            return $this->getErrorMessage($event);
        }
        Catch(ServiceNotFound $e) {
            return $this->getErrorMessage($event);
        }
    }

    private function getErrorMessage(ResourceEvent $event)
    {
        $message = $this->errorMessage->get($event);
        return new ApiProblem(405, $message);
{'fraction_non_alphanumeric': 0.08962597035991532, 'fraction_numerical': 0.002117148906139732, 'mean_word_length': 3.9649737302977233, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, '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': '4882663', 'n_tokens_mistral': 790, 'n_tokens_neox': 747, 'n_words': 142}