Completed
Pull Request — master (#17)
by Alexander
06:45
created

Message::body()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace alkemann\h2l;
4
5
/**
6
 * Class Message
7
 *
8
 * Container for requests and responses made with the Remote class
9
 *
10
 * @package alkemann\h2l
11
 */
12
final class Message
13
{
14
    const CONTENT_JSON = 'application/json';
15
    const CONTENT_FORM = 'application/x-www-form-urlencoded';
16
    const CONTENT_HTML = 'text/html';
17
    const CONTENT_TEXT = 'text/plain';
18
    const CONTENT_XML = 'text/xml';
19
20
    const REQUEST = "REQUEST";
21
    const RESPONSE = "RESPONSE";
22
    /**
23
     * @var string  "REQUEST"|"RESPONSE"
24
     */
25
    private $type;
26
27
    /**
28
     * @var int
29
     */
30
    private $code;
31
    /**
32
     * @var string
33
     */
34
    private $url = '';
35
    /**
36
     * Enum with Request::GET, Request::POST etc
37
     * @var string
38
     */
39
    private $method = Request::GET;
40
    /**
41
     * @var ?string
42
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment ?string at position 0 could not be parsed: Unknown type name '?string' at position 0 in ?string.
Loading history...
43
    private $body;
44
    /**
45
     * @var array
46
     */
47
    private $meta = [];
48
    /**
49
     * @var array
50
     */
51
    private $headers = [];
52
    /**
53
     * @var array
54
     */
55
    private $options = [];
56
    /**
57
     * @var string
58
     */
59
    private $content_type = 'text/html';
60
    /**
61
     * @var string
62
     */
63
    private $content_charset = 'utf-8';
64
65
    // public function __construct() {}
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66
67
    public function type(): ?string
68
    {
69
        return $this->type;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function url(): string
76
    {
77
        return $this->url;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function method(): string
84
    {
85
        return $this->method;
86
    }
87
88
    /**
89
     * @return null|string
90
     */
91
    public function body(): ?string
92
    {
93
        return $this->body;
94
    }
95
96
    /**
97
     * @return null|string|array|SimpleXMLElement body converted from raw format
0 ignored issues
show
Bug introduced by
The type alkemann\h2l\SimpleXMLElement was not found. Did you mean SimpleXMLElement? If so, make sure to prefix the type with \.
Loading history...
98
     */
99
    public function content()
100
    {
101
        switch ($this->contentType()) {
102
            case static::CONTENT_JSON:
1 ignored issue
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
103
                return json_decode($this->body, true);
104
            case static::CONTENT_XML:
105
                return new \SimpleXMLElement($this->body);
106
            case null:
107
            default:
108
                return $this->body;
109
        }
110
    }
111
112
    public function contentType(): string
113
    {
114
        return $this->content_type;
115
    }
116
117
    public function charset(): string
118
    {
119
        return $this->content_charset;
120
    }
121
122
    /**
123
     * @param string $name
124
     * @return null|string
125
     */
126
    public function header(string $name): ?string
127
    {
128
        foreach ($this->headers as $key => $value) {
129
            if (strcasecmp($key, $name) === 0) {
130
                return $value;
131
            }
132
        }
133
        return null;
134
    }
135
136
    /**
137
     * @param string $class name of class that must take data array as constructor
138
     * @return object body json decoded and sent to constructor of $class
139
     */
140
    public function as(string $class)
141
    {
142
        return new $class($this->content());
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function headers(): array
149
    {
150
        return $this->headers;
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function meta(): array
157
    {
158
        return $this->meta;
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function options(): array
165
    {
166
        return $this->options;
167
    }
168
169
    /**
170
     * @return int|null
171
     */
172
    public function code(): ?int
173
    {
174
        return $this->code;
175
    }
176
177
    /**
178
     * @param string $type
179
     * @return Message
180
     */
181
    public function withType(string $type): Message
182
    {
183
        $new = clone $this;
184
        $new->type = $type;
185
        return $new;
186
    }
187
188
    /**
189
     * @param int $code
190
     * @return Message
191
     */
192
    public function withCode(int $code): Message
193
    {
194
        $new = clone $this;
195
        $new->code = $code;
196
        return $new;
197
    }
198
199
    /**
200
     * @param string $url
201
     * @return Message
202
     */
203
    public function withUrl(string $url): Message
204
    {
205
        $new = clone $this;
206
        $new->url = $url;
207
        return $new;
208
    }
209
210
    /**
211
     * @param string $method
212
     * @return Message
213
     */
214
    public function withMethod(string $method): Message
215
    {
216
        $new = clone $this;
217
        $new->method = $method;
218
        return $new;
219
    }
220
221
    /**
222
     * @param string $body
223
     * @return Message
224
     */
225
    public function withBody(string $body): Message
226
    {
227
        $new = clone $this;
228
        $new->body = $body;
229
        return $new;
230
    }
231
232
    /**
233
     * @param array $headers
234
     * @return Message
235
     */
236
    public function withHeaders(array $headers): Message
237
    {
238
        $new = clone $this;
239
        $new->headers = $headers;
240
        $content_header = $new->header('Content-Type');
241
        if ($content_header) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $content_header of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
242
            if (strpos($content_header, ';') === false) {
243
                $new->content_type = trim(strtolower($content_header));
244
            } else {
245
                list($type, $other) = explode(';', $content_header, 2);
246
                $new->content_type = trim(strtolower($type));
247
                list($key, $charset) = explode('=', $other, 2);
248
                if ('charset' === strtolower(trim($key))) {
249
                    $new->content_charset = strtolower(trim(trim($charset, '"')));
250
                }
251
            }
252
        }
253
        return $new;
254
    }
255
256
    /**
257
     * @param array $options
258
     * @return Message
259
     */
260
    public function withOptions(array $options): Message
261
    {
262
        $new = clone $this;
263
        $new->options = $options;
264
        return $new;
265
    }
266
267
    /**
268
     * @param array $meta
269
     * @return Message
270
     */
271
    public function withMeta(array $meta): Message
272
    {
273
        $new = clone $this;
274
        $new->meta = $meta;
275
        return $new;
276
    }
277
}
278