Passed
Push — master ( 12b4e1...e176ef )
by Lynh
13:05
created

Response::header()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Response::isError() 0 3 1
1
<?php
2
3
namespace Jenky\Hermes;
4
5
use GuzzleHttp\Psr7\Response as GuzzleResponse;
6
use Illuminate\Support\Traits\Macroable;
7
use Jenky\Hermes\Concerns\InteractsWithMessage;
8
use Jenky\Hermes\Contracts\HttpResponseHandler;
9
use Psr\Http\Message\ResponseInterface;
10
11
class Response extends GuzzleResponse implements HttpResponseHandler
12
{
13
    use InteractsWithMessage, Macroable;
1 ignored issue
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Jenky\Hermes\Response.
Loading history...
14
15
    /**
16
     * Create new response handler instance.
17
     *
18
     * @param  \Psr\Http\Message\ResponseInterface $response
19
     * @return \Psr\Http\Message\ResponseInterface
20
     */
21
    public static function create(ResponseInterface $response): ResponseInterface
22
    {
23
        $handler = new static(
24
            $response->getStatusCode(),
25
            $response->getHeaders(),
26
            $response->getBody(),
27
            $response->getProtocolVersion(),
28
            $response->getReasonPhrase()
29
        );
30
31
        if ($handler instanceof Parsable) {
32
            $handler->parse();
1 ignored issue
show
Bug introduced by
The method parse() does not exist on Jenky\Hermes\Response. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
            $handler->/** @scrutinizer ignore-call */ 
33
                      parse();
Loading history...
33
        }
34
35
        return $handler;
36
    }
37
38
    /**
39
     * Determine that request is successful.
40
     *
41
     * @return bool
42
     */
43
    public function isSuccessful(): bool
44
    {
45
        $statusCode = $this->getStatusCode();
46
47
        return ($statusCode >= 200 && $statusCode < 300) || $statusCode == 304;
48
    }
49
50
    /**
51
     * Determine that request is error.
52
     *
53
     * @return bool
54
     */
55
    public function isError(): bool
56
    {
57
        return ! $this->isSuccessful();
58
    }
59
60
    /**
61
     * Checks if HTTP Status code is Information (1xx).
62
     *
63
     * @return bool
64
     */
65
    public function isInformational(): bool
66
    {
67
        return $this->getStatusCode() < 200;
68
    }
69
70
    /**
71
     * Checks if HTTP Status code is a Redirect (3xx).
72
     *
73
     * @return bool
74
     */
75
    public function isRedirect(): bool
76
    {
77
        $statusCode = $this->getStatusCode();
78
79
        return $statusCode >= 300 && $statusCode < 400;
80
    }
81
82
    /**
83
     * Checks if HTTP Status code is a Client Error (4xx).
84
     *
85
     * @return bool
86
     */
87
    public function isClientError(): bool
88
    {
89
        $statusCode = $this->getStatusCode();
90
91
        return $statusCode >= 400 && $statusCode < 500;
92
    }
93
94
    /**
95
     * Checks if HTTP Status code is a Server Error (5xx).
96
     *
97
     * @return bool
98
     */
99
    public function isServerError(): bool
100
    {
101
        return $this->getStatusCode() >= 500;
102
    }
103
}
104