Completed
Push — master ( 2b4de6...20220d )
by Matthew
02:17
created

ApiException::create()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 24
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 5.2164
cc 10
eloc 22
nc 10
nop 1

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
 * Created by PhpStorm.
4
 * User: Matt
5
 */
6
7
namespace Freshdesk\Exceptions;
8
9
use Exception;
10
use GuzzleHttp\Exception\RequestException;
11
12
/**
13
 * General Exception
14
 *
15
 * Thrown when the Freshdesk API returns an HTTP error code that isn't handled by other exceptions
16
 *
17
 * @package Exceptions
18
 * @author Matthew Clarkson <[email protected]>
19
 */
20
class ApiException extends Exception
21
{
22
23
    /**
24
     * @internal
25
     * @param RequestException $e
26
     * @return AccessDeniedException|ApiException|AuthenticationException|ConflictingStateException|
27
     * MethodNotAllowedException|NotFoundException|RateLimitExceededException|UnsupportedAcceptHeaderException|
28
     * UnsupportedContentTypeException|ValidationException
29
     */
30
    static function create(RequestException $e) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
        switch ($e->getResponse()->getStatusCode()) {
32
            case 400:
33
                return new ValidationException($e);
34
            case 401:
35
                return new AuthenticationException($e);
36
            case 403:
37
                return new AccessDeniedException($e);
38
            case 404:
39
                return new NotFoundException($e);
40
            case 405:
41
                return new MethodNotAllowedException($e);
42
            case 406:
43
                return new UnsupportedAcceptHeaderException($e);
44
            case 409:
45
                return new ConflictingStateException($e);
46
            case 415:
47
                return new UnsupportedContentTypeException($e);
48
            case 429:
49
                return new RateLimitExceededException($e);
50
            default:
51
                return new ApiException($e);
52
        }
53
    }
54
55
    /**
56
     * @var RequestException
57
     * @internal
58
     */
59
    private $exception;
60
61
    /**
62
     * Returns the Request Exception
63
     *
64
     * A Guzzle Request Exception is returned
65
     *
66
     * @return RequestException
67
     */
68
    public function getRequestException()
69
    {
70
        return $this->exception;
71
    }
72
73
    /**
74
     * Exception constructor
75
     *
76
     * Constructs a new exception.
77
     *
78
     * @param RequestException $e
79
     * @internal
80
     */
81
    public function __construct(RequestException $e)
82
    {
83
        $this->exception = $e;
84
        parent::__construct();
85
    }
86
}
87