ResponseMediator   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 126
rs 10
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isServerError() 0 3 1
A isRedirect() 0 3 2
A getContent() 0 17 4
A isInformational() 0 3 2
A isClientError() 0 3 2
A isSuccess() 0 3 2
A isError() 0 3 2
A isOk() 0 3 1
A isConflictError() 0 3 1
1
<?php
2
3
namespace Transmission\HttpClient\Message;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Utilities to parse response headers and content.
9
 */
10
class ResponseMediator
11
{
12
    /**
13
     * Check status code for informational response.
14
     *
15
     * @param ResponseInterface $response
16
     *
17
     * @return bool
18
     */
19
    public static function isInformational(ResponseInterface $response): bool
20
    {
21
        return $response->getStatusCode() >= 100 && $response->getStatusCode() < 200;
22
    }
23
24
    /**
25
     * Check status code for success.
26
     *
27
     * @param ResponseInterface $response
28
     *
29
     * @return bool
30
     */
31
    public static function isSuccess(ResponseInterface $response): bool
32
    {
33
        return $response->getStatusCode() >= 200 && $response->getStatusCode() < 300;
34
    }
35
36
    /**
37
     * Alias of static::isSuccess().
38
     *
39
     * @see static::isSuccess()
40
     *
41
     * @param ResponseInterface $response
42
     *
43
     * @return bool
44
     */
45
    public static function isOk(ResponseInterface $response): bool
46
    {
47
        return static::isSuccess($response);
48
    }
49
50
    /**
51
     * Check status code for redirect.
52
     *
53
     * @param ResponseInterface $response
54
     *
55
     * @return bool
56
     */
57
    public static function isRedirect(ResponseInterface $response): bool
58
    {
59
        return $response->getStatusCode() >= 300 && $response->getStatusCode() < 400;
60
    }
61
62
    /**
63
     * Check status code for errors.
64
     *
65
     * @see static::isSuccess()
66
     *
67
     * @param ResponseInterface $response
68
     *
69
     * @return bool
70
     */
71
    public static function isError(ResponseInterface $response): bool
72
    {
73
        return static::isClientError($response) || static::isServerError($response);
74
    }
75
76
    /**
77
     * Check status code for client error.
78
     *
79
     * @param ResponseInterface $response
80
     *
81
     * @return bool
82
     */
83
    public static function isClientError(ResponseInterface $response): bool
84
    {
85
        return $response->getStatusCode() >= 400 && $response->getStatusCode() < 500;
86
    }
87
88
    /**
89
     * Check status code for server error.
90
     *
91
     * @param ResponseInterface $response
92
     *
93
     * @return bool
94
     */
95
    public static function isServerError(ResponseInterface $response): bool
96
    {
97
        return $response->getStatusCode() >= 500;
98
    }
99
100
    /**
101
     * Check status code for conflict error.
102
     *
103
     * @param ResponseInterface $response
104
     *
105
     * @return bool
106
     */
107
    public static function isConflictError(ResponseInterface $response): bool
108
    {
109
        return $response->getStatusCode() === 409;
110
    }
111
112
    /**
113
     * Return the response body as a string or json array if content type is application/json.
114
     *
115
     * @param ResponseInterface $response
116
     *
117
     * @return array|string
118
     */
119
    public static function getContent(ResponseInterface $response)
120
    {
121
        $body = $response->getBody()->__toString();
122
123
        if (static::isError($response)) {
124
            // Transmission returns error messages with HTML.
125
            return strip_tags($body);
126
        }
127
128
        if (strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0) {
129
            $content = json_decode($body, true);
130
            if (JSON_ERROR_NONE === json_last_error()) {
131
                return $content;
132
            }
133
        }
134
135
        return $body;
136
    }
137
}
138