ErrorListener   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 0
cbo 7
dl 0
loc 65
ccs 0
cts 36
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C onRequestError() 0 59 16
1
<?php
2
3
namespace Trello\HttpClient\Listener;
4
5
use Trello\HttpClient\Message\ResponseMediator;
6
use Guzzle\Common\Event;
7
use Trello\Exception\ErrorException;
8
use Trello\Exception\RuntimeException;
9
use Trello\Exception\PermissionDeniedException;
10
use Trello\Exception\ValidationFailedException;
11
use Trello\Exception\ApiLimitExceedException;
12
13
/**
14
 * @TODO Map real errors from Trello API
15
 */
16
class ErrorListener
17
{
18
    /**
19
     * {@inheritDoc}
20
     */
21
    public function onRequestError(Event $event)
22
    {
23
        /** @var $request \Guzzle\Http\Message\Request */
24
        $request = $event['request'];
25
        $response = $request->getResponse();
26
27
        if (!$response->isClientError() && !$response->isServerError()) {
28
            return;
29
        }
30
31
        switch ($response->getStatusCode()) {
32
            case 429:
33
                throw new ApiLimitExceedException('Wait a second.', 429);
34
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
35
        }
36
37
        $content = ResponseMediator::getContent($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $request->getResponse() on line 25 can be null; however, Trello\HttpClient\Messag...eMediator::getContent() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
38
        if (is_array($content) && isset($content['message'])) {
39
            if (400 == $response->getStatusCode()) {
40
                throw new ErrorException($content['message'], 400);
41
            }
42
43
            if (401 == $response->getStatusCode()) {
44
                throw new PermissionDeniedException($content['message'], 401);
45
            }
46
47
            if (422 == $response->getStatusCode() && isset($content['errors'])) {
48
                $errors = array();
49
                foreach ($content['errors'] as $error) {
50
                    switch ($error['code']) {
51
                    case 'missing':
52
                        $errors[] = sprintf('The %s %s does not exist, for resource "%s"', $error['field'], $error['value'], $error['resource']);
53
                        break;
54
55
                    case 'missing_field':
56
                        $errors[] = sprintf('Field "%s" is missing, for resource "%s"', $error['field'], $error['resource']);
57
                        break;
58
59
                    case 'invalid':
60
                        $errors[] = sprintf('Field "%s" is invalid, for resource "%s"', $error['field'], $error['resource']);
61
                        break;
62
63
                    case 'already_exists':
64
                        $errors[] = sprintf('Field "%s" already exists, for resource "%s"', $error['field'], $error['resource']);
65
                        break;
66
67
                    default:
68
                        $errors[] = $error['message'];
69
                        break;
70
71
                    }
72
                }
73
74
                throw new ValidationFailedException('Validation Failed: '.implode(', ', $errors), 422);
75
            }
76
        }
77
78
        throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
79
    }
80
}
81