Completed
Push — master ( 68730f...39166d )
by Zbigniew
02:26
created

transformByStatusCodeAndName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 7
nc 3
nop 3
crap 3
1
<?php
2
/**
3
 * This file is part of the WrikePhpLibrary package.
4
 *
5
 * (c) Zbigniew Ślązak
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zibios\WrikePhpLibrary\Transformer;
12
13
use Zibios\WrikePhpLibrary\Exception\Api\AccessForbiddenException;
14
use Zibios\WrikePhpLibrary\Exception\Api\ApiException;
15
use Zibios\WrikePhpLibrary\Exception\Api\InvalidParameterException;
16
use Zibios\WrikePhpLibrary\Exception\Api\InvalidRequestException;
17
use Zibios\WrikePhpLibrary\Exception\Api\MethodNotFoundException;
18
use Zibios\WrikePhpLibrary\Exception\Api\NotAllowedException;
19
use Zibios\WrikePhpLibrary\Exception\Api\NotAuthorizedException;
20
use Zibios\WrikePhpLibrary\Exception\Api\ParameterRequiredException;
21
use Zibios\WrikePhpLibrary\Exception\Api\ResourceNotFoundException;
22
use Zibios\WrikePhpLibrary\Exception\Api\ServerErrorException;
23
24
/**
25
 * Abstract Api Exception Transformer
26
 */
27
abstract class AbstractApiExceptionTransformer implements ApiExceptionTransformerInterface
28
{
29
    /**
30
     * @var array
31
     */
32
    protected $supportedApiExceptions = [
33
        AccessForbiddenException::class,
34
        InvalidParameterException::class,
35
        InvalidRequestException::class,
36
        MethodNotFoundException::class,
37
        NotAllowedException::class,
38
        NotAuthorizedException::class,
39
        ParameterRequiredException::class,
40
        ResourceNotFoundException::class,
41
        ServerErrorException::class,
42
    ];
43
44
    /**
45
     * @param \Exception $exception
46
     * @param int $errorStatusCode
47
     * @param string $errorStatusName
48
     *
49
     * @return ApiException
50
     */
51 19
    protected function transformByStatusCodeAndName(\Exception $exception, $errorStatusCode, $errorStatusName)
52
    {
53
54 19
        $incomingIdentifier = ApiException::calculateExceptionIdentifier($errorStatusCode, $errorStatusName);
55 19
        foreach ($this->supportedApiExceptions as $apiExceptionClass) {
56 19
            $supportedIdentifier = call_user_func([$apiExceptionClass, 'getExceptionIdentifier']);
57 19
            if ($incomingIdentifier === $supportedIdentifier) {
58 19
                return new $apiExceptionClass($exception);
59
            }
60
        }
61
62 10
        return new ApiException($exception);
63
    }
64
}
65