Completed
Push — master ( deea37...cc8ae8 )
by Zbigniew
04:07
created

ApiException::create()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 12.0065

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 27
cts 28
cp 0.9643
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 28
nc 11
nop 1
crap 12.0065

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the WrikePhpSdk 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\WrikePhpSdk\Exceptions\Api;
12
13
use Exception;
14
use GuzzleHttp\Exception\ClientException;
15
use GuzzleHttp\Exception\ServerException;
16
17
/**
18
 * General Wrike Api Exception
19
 *
20
 * Thrown when the Wrike API returns an HTTP error that isn't handled by other dedicated exceptions.
21
 */
22
class ApiException extends Exception
23
{
24
    /** @var  \Exception */
25
    protected $responseException;
26
27
    /**
28
     * ApiException constructor.
29
     *
30
     * @param Exception $e
31
     */
32 27
    public function __construct(\Exception $e)
33
    {
34 27
        parent::__construct($e->getMessage(), $e->getCode(), $e);
35 27
    }
36
37
    /**
38
     * @param \Exception $e
39
     *
40
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use ApiException.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
41
     */
42 17
    public static function create(\Exception $e)
43
    {
44 17
        if ($e instanceof ClientException === false && $e instanceof ServerException === false) {
45
            return new ApiException($e);
46
        }
47
        /** @var ClientException $e */
48 17
        $errorResponse = $e->getResponse();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
49 17
        $bodyString = (string) $errorResponse->getBody();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
50 17
        $body = json_decode($bodyString, true);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
51 17
        $errorStatusCode = $errorResponse->getStatusCode();
52 17
        $errorName = $body['error'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
53
54 17
        switch ($errorStatusCode . $errorName) {
55 17
            case (AccessForbiddenException::STATUS_CODE . AccessForbiddenException::STATUS_NAME):
56 1
                return new AccessForbiddenException($e);
57 16
            case (InvalidParameterException::STATUS_CODE . InvalidParameterException::STATUS_NAME):
58 1
                return new InvalidParameterException($e);
59 15
            case (InvalidRequestException::STATUS_CODE . InvalidRequestException::STATUS_NAME):
60 1
                return new InvalidRequestException($e);
61 14
            case (MethodNotFoundException::STATUS_CODE . MethodNotFoundException::STATUS_NAME):
62 1
                return new MethodNotFoundException($e);
63 13
            case (NotAllowedException::STATUS_CODE . NotAllowedException::STATUS_NAME):
64 1
                return new NotAllowedException($e);
65 12
            case (NotAuthorizedException::STATUS_CODE . NotAuthorizedException::STATUS_NAME):
66 1
                return new NotAuthorizedException($e);
67 11
            case (ParameterRequiredException::STATUS_CODE . ParameterRequiredException::STATUS_NAME):
68 1
                return new ParameterRequiredException($e);
69 10
            case (ResourceNotFoundException::STATUS_CODE . ResourceNotFoundException::STATUS_NAME):
70 1
                return new ResourceNotFoundException($e);
71 9
            case (ServerErrorException::STATUS_CODE . ServerErrorException::STATUS_NAME):
72 1
                return new ServerErrorException($e);
73
        }
74
75 8
        return new ApiException($e);
76
    }
77
}
78