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

InteractsWithMessage::serverError()   A

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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Jenky\Hermes\Concerns;
4
5
trait InteractsWithMessage
6
{
7
    /**
8
     * Retrieve a header from the request.
9
     *
10
     * @param  string|null  $key
11
     * @param  string|array|null  $default
12
     * @return string|array|null
13
     */
14
    public function header($header = null, $default = null)
15
    {
16
        if ($header) {
17
            return $this->getHeader($header)[0] ?? $default;
1 ignored issue
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

17
            return $this->/** @scrutinizer ignore-call */ getHeader($header)[0] ?? $default;
Loading history...
18
        }
19
20
        return array_map(function ($values) {
21
            return $values[0] ?? null;
22
        }, $this->getHeaders());
1 ignored issue
show
Bug introduced by
It seems like getHeaders() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

22
        }, $this->/** @scrutinizer ignore-call */ getHeaders());
Loading history...
23
    }
24
25
    /**
26
     * Get or check the status code.
27
     *
28
     * @param  int|null $code
29
     * @return int|bool
30
     */
31
    public function status($code = null)
32
    {
33
        $statusCode = $this->getStatusCode();
1 ignored issue
show
Bug introduced by
It seems like getStatusCode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

33
        /** @scrutinizer ignore-call */ 
34
        $statusCode = $this->getStatusCode();
Loading history...
34
35
        return $code ? $statusCode == $code : $statusCode;
36
    }
37
38
    /**
39
     * Get the response body as string.
40
     *
41
     * @return string
42
     */
43
    public function body()
44
    {
45
        return (string) $this->getBody();
1 ignored issue
show
Bug introduced by
It seems like getBody() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

45
        return (string) $this->/** @scrutinizer ignore-call */ getBody();
Loading history...
46
    }
47
48
    /**
49
     * Determine that response status code is ok.
50
     *
51
     * @return bool
52
     */
53
    public function ok(): bool
54
    {
55
        return $this->status(200);
56
    }
57
58
    /**
59
     * Determine that response status code is created.
60
     *
61
     * @return bool
62
     */
63
    public function created(): bool
64
    {
65
        return $this->status(201);
66
    }
67
68
    /**
69
     * Determine that response status code is bad request.
70
     *
71
     * @return bool
72
     */
73
    public function badRequest(): bool
74
    {
75
        return $this->status(400);
76
    }
77
78
    /**
79
     * Determine that response status code is unauthorized.
80
     *
81
     * @return bool
82
     */
83
    public function unauthorized(): bool
84
    {
85
        return $this->status(401);
86
    }
87
88
    /**
89
     * Determine that response status code is forbidden.
90
     *
91
     * @return bool
92
     */
93
    public function forbidden(): bool
94
    {
95
        return $this->status(403);
96
    }
97
98
    /**
99
     * Determine that response status code is not found.
100
     *
101
     * @return bool
102
     */
103
    public function notFound(): bool
104
    {
105
        return $this->status(404);
106
    }
107
108
    /**
109
     * Determine that response status code is unprocessable.
110
     *
111
     * @return bool
112
     */
113
    public function unprocessable(): bool
114
    {
115
        return $this->status(422);
116
    }
117
118
    /**
119
     * Determine that response status code is internal server error.
120
     *
121
     * @return bool
122
     */
123
    public function serverError(): bool
124
    {
125
        return $this->status(500);
126
    }
127
}
128