CheckResponse::getResponseHeader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Jasny\Controller\Traits;
5
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Methods to check the response
10
 */
11
trait CheckResponse
12
{
13
    abstract protected function getResponse(): ResponseInterface;
14
15
    /**
16
     * Get the response status code
17
     */
18
    protected function getStatusCode(): int
19
    {
20
        return $this->getResponse()->getStatusCode() ?: 200;
21
    }
22
23
    /**
24
     * Get the value of the response header, or an empty string if not set.
25
     */
26
    protected function getResponseHeader(string $name): string
27
    {
28
        return $this->getResponse()->getHeaderLine($name);
29
    }
30
31
    
32
    /**
33
     * Check if response is 1xx informational
34
     */
35
    protected function isInformational(): bool
36
    {
37
        $code = $this->getStatusCode();
38
        return $code >= 100 && $code < 200;
39
    }
40
    
41
    /**
42
     * Check if response is 2xx successful (or empty)
43
     */
44
    protected function isSuccessful(): bool
45
    {
46
        $code = $this->getStatusCode();
47
        return $code >= 200 && $code < 300;
48
    }
49
    
50
    /**
51
     * Check if response is a 3xx redirect
52
     */
53
    protected function isRedirection(): bool
54
    {
55
        $code = $this->getStatusCode();
56
        return $code >= 300 && $code < 400;
57
    }
58
    
59
    /**
60
     * Check if response is a 4xx client error
61
     */
62
    protected function isClientError(): bool
63
    {
64
        $code = $this->getStatusCode();
65
        return $code >= 400 && $code < 500;
66
    }
67
    
68
    /**
69
     * Check if response is a 5xx server error
70
     */
71
    protected function isServerError(): bool
72
    {
73
        $code = $this->getStatusCode();
74
        return $code >= 500 && $code < 600;
75
    }
76
77
    /**
78
     * Check if response is 4xx or 5xx error
79
     */
80
    protected function isError(): bool
81
    {
82
        return $this->isClientError() || $this->isServerError();
83
    }
84
}
85