Passed
Push — master ( 8bdcbb...696730 )
by Zbigniew
03:20
created

AbstractApiExceptionTransformer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
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 37
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

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