ExceptionMiddleware::execute()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.6111
cc 5
nc 5
nop 2
crap 5
1
<?php
2
3
namespace DMT\WebservicesNl\Client\Command\Middleware;
4
5
use DMT\CommandBus\Validator\ValidationException;
6
use DMT\WebservicesNl\Client\Exception\ExceptionInterface;
7
use DMT\WebservicesNl\Client\Exception\ExceptionHandler;
8
use GuzzleHttp\Exception\RequestException;
9
use League\Tactician\Middleware;
10
11
/**
12
 * Class ExceptionMiddleware
13
 *
14
 * @package DMT\WebservicesNl\Client
15
 */
16
class ExceptionMiddleware implements Middleware
17
{
18
    /**
19
     * @var ExceptionHandler
20
     */
21
    protected $exceptionHandler;
22
23
    /**
24
     * ExceptionMiddleware constructor.
25
     */
26 20
    public function __construct()
27
    {
28 20
        $this->exceptionHandler = new ExceptionHandler();
29 20
    }
30
31
    /**
32
     * @param object $command
33
     * @param callable $next
34
     *
35
     * @return mixed
36
     * @throws ExceptionInterface
37
     */
38 18
    public function execute($command, callable $next)
39
    {
40
        try {
41 18
            return $next($command);
42 18
        } catch (ExceptionInterface $exception) {
43 4
            throw $exception;
44 14
        } catch (ValidationException $exception) {
45 1
            $this->exceptionHandler->throwServiceExceptionFromViolationException($exception);
46 13
        } catch (RequestException $exception) {
47 6
            $this->exceptionHandler->throwServiceExceptionFromRequestException($exception);
48 7
        } catch (\Throwable $exception) {
49 7
            $this->exceptionHandler->throwServiceException($exception);
50
        }
51
    }
52
}
53