AbstractApiExceptionTransformer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 40
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A transformByStatusCodeAndName() 0 15 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the zibios/wrike-php-library package.
7
 *
8
 * (c) Zbigniew Ślązak
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zibios\WrikePhpLibrary\Transformer\ApiException;
15
16
use Zibios\WrikePhpLibrary\Exception\Api\AccessForbiddenException;
17
use Zibios\WrikePhpLibrary\Exception\Api\ApiException;
18
use Zibios\WrikePhpLibrary\Exception\Api\InvalidParameterException;
19
use Zibios\WrikePhpLibrary\Exception\Api\InvalidRequestException;
20
use Zibios\WrikePhpLibrary\Exception\Api\MethodNotFoundException;
21
use Zibios\WrikePhpLibrary\Exception\Api\NotAllowedException;
22
use Zibios\WrikePhpLibrary\Exception\Api\NotAuthorizedException;
23
use Zibios\WrikePhpLibrary\Exception\Api\ParameterRequiredException;
24
use Zibios\WrikePhpLibrary\Exception\Api\ResourceNotFoundException;
25
use Zibios\WrikePhpLibrary\Exception\Api\ServerErrorException;
26
use Zibios\WrikePhpLibrary\Transformer\ApiExceptionTransformerInterface;
27
28
/**
29
 * Abstract Api Exception Transformer.
30
 */
31
abstract class AbstractApiExceptionTransformer implements ApiExceptionTransformerInterface
32
{
33
    /**
34
     * @var array
35
     */
36
    protected $supportedApiExceptions = [
37
        AccessForbiddenException::class,
38
        InvalidParameterException::class,
39
        InvalidRequestException::class,
40
        MethodNotFoundException::class,
41
        NotAllowedException::class,
42
        NotAuthorizedException::class,
43
        ParameterRequiredException::class,
44
        ResourceNotFoundException::class,
45
        ServerErrorException::class,
46
    ];
47
48
    /**
49
     * @param \Throwable $exception
50
     * @param int        $errorStatusCode
51
     * @param string     $errorStatusName
52
     *
53
     * @return ApiException
54
     */
55 19
    protected function transformByStatusCodeAndName(
56
        \Throwable $exception,
57
        $errorStatusCode,
58
        $errorStatusName
59
    ): ApiException {
60 19
        foreach ($this->supportedApiExceptions as $apiExceptionClass) {
61 19
            $statusCode = \constant($apiExceptionClass . '::STATUS_CODE');
62 19
            $statusName = \constant($apiExceptionClass . '::STATUS_NAME');
63 19
            if ($errorStatusCode === $statusCode && $errorStatusName === $statusName) {
64 19
                return new $apiExceptionClass($exception);
65
            }
66
        }
67
68 10
        return new ApiException($exception);
69
    }
70
}
71