ExceptionHandler::getServiceException()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace DMT\WebservicesNl\Client\Exception;
4
5
use DMT\CommandBus\Validator\ValidationException;
6
use DMT\WebservicesNl\Client\Exception\Client\InputException;
7
use DMT\WebservicesNl\Client\Exception\Server\Unavailable\InternalErrorException;
8
use GuzzleHttp\Exception\RequestException;
9
10
/**
11
 * Class ExceptionHandler
12
 *
13
 * @package DMT\WebservicesNl\Client
14
 */
15
class ExceptionHandler
16
{
17
    /**
18
     * @param \Throwable $exception
19
     *
20
     * @throws ServerException|ExceptionInterface
21
     */
22 28
    public function throwServiceException(\Throwable $exception)
23
    {
24 28
        $exceptionClass = ExceptionInterface::class;
25
26 28
        if (method_exists($exception, 'getFaultCode')) {
27 26
            $exceptionClass = $this->getServiceException($exception->getFaultCode());
28
        }
29 28
        if ($exceptionClass !== ExceptionInterface::class) {
30 24
            throw new $exceptionClass($exception->getMessage());
31
        }
32
33 4
        throw new ServerException('Unknown error occurred', 0, $exception);
34
    }
35
36
    /**
37
     * @param ValidationException $exception
38
     *
39
     * @throws InputException
40
     */
41 4
    public function throwServiceExceptionFromViolationException(ValidationException $exception)
42
    {
43 4
        $message = 'Invalid input given';
44
45 4
        if ($exception->getViolations()->count() === 1) {
46 2
            $message = $exception->getViolations()->get(0)->getMessage();
47
        }
48
49 4
        throw new InputException($message, 0, $exception);
50
    }
51
52
    /**
53
     * @param RequestException $exception
54
     *
55
     * @throws InternalErrorException|ExceptionInterface
56
     */
57 25
    public function throwServiceExceptionFromRequestException(RequestException $exception)
58
    {
59 25
        $exceptionClass = ExceptionInterface::class;
60
61 25
        if ($exception->hasResponse() && $exception->getResponse()->hasHeader('X-WS-ErrorCode')) {
62 24
            $exceptionClass = $this->getServiceException($exception->getResponse()->getHeaderLine('X-WS-ErrorCode'));
63
        }
64 25
        if ($exceptionClass !== ExceptionInterface::class) {
65 22
            throw new $exceptionClass();
66
        }
67
68 3
        throw new ServerException('Unknown error occurred', 0, $exception);
69
    }
70
71
    /**
72
     * @param string $faultCode
73
     * @return string
74
     */
75 50
    protected function getServiceException(string $faultCode = 'Server'): string
76
    {
77 50
        $codes = explode('.', $faultCode);
78 50
        $currentException = ExceptionInterface::class;
79
80 50
        foreach ($codes as $code) {
81 50
            $namespace = preg_replace('~\\\?Exception(Interface)?$~', '', $currentException);
82 50
            $childException = $namespace  . '\\' . $code . 'Exception';
83
84 50
            if (!class_exists($childException)) {
85 6
                break;
86
            }
87 46
            $currentException = $childException;
88
        }
89
90 50
        return $currentException;
91
    }
92
}
93