Completed
Push — master ( 4fd434...e1a522 )
by Bohuslav
03:35
created

Request::__construct()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 4
Bugs 3 Features 0
Metric Value
c 4
b 3
f 0
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 8.5806
cc 4
eloc 18
nc 5
nop 5
crap 4
1
<?php
2
namespace Kambo\Http\Message;
3
4
// \Spl
5
use InvalidArgumentException;
6
7
// \Psr
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\UriInterface;
10
11
// \Http\Message
12
use Kambo\Http\Message\Message;
13
use Kambo\Http\Message\Headers;
14
use Kambo\Http\Message\RequestTrait;
15
use Kambo\Http\Message\Factories\String\UriFactory;
16
use Kambo\Http\Message\Stream;
17
18
/**
19
 * Representation of an outgoing, client-side request.
20
 *
21
 * Per the HTTP specification, this interface includes properties for
22
 * each of the following:
23
 *
24
 * - Protocol version
25
 * - HTTP method
26
 * - URI
27
 * - Headers
28
 * - Message body
29
 *
30
 * During construction, implementations attempt to set the Host header from
31
 * a provided URI if no Host header is provided.
32
 *
33
 * Requests are considered immutable; all methods that change state retain
34
 * the internal state of the current message and return an instance that
35
 * contains the changed state.
36
 *
37
 * @package Kambo\Http\Message
38
 * @author  Bohuslav Simek <[email protected]>
39
 * @license MIT
40
 */
41
class Request extends Message implements RequestInterface
42
{
43
    use RequestTrait;
44
45
    /**
46
     * Create new outgoing HTTP request.
47
     *
48
     * Adds a host header when none was provided and a host is defined in URI.
49
     *
50
     * @param string                      $requestMethod The request method
51
     * @param UriInterface|string         $uri           The request URI object
52
     * @param HeadersInterface            $headers       The request headers collection
53
     * @param StreamInterface|string|null $body          The request body object
54
     * @param string                      $protocol      The request version of the protocol
55
     *
56
     * @throws \InvalidArgumentException If an unsupported argument type is
57
     *                                   provided for URI or body.
58
     */
59 13
    public function __construct(
60
        $requestMethod,
61
        $uri,
62
        $headers = null,
63
        $body = null,
64
        $protocol = '1.1'
65
    ) {
66 13
        parent::__construct($headers, $body, $protocol);
67
68 12
        if (is_string($uri)) {
69 10
            $this->uri = (new UriFactory())->create($uri);
70 12
        } elseif ($uri instanceof UriInterface) {
71 1
            $this->uri = $uri;
72 1
        } else {
73 1
            throw new InvalidArgumentException(
74
                'URI must be a string or implement Psr\Http\Message\UriInterface'
75 1
            );
76
        }
77
78 11
        $this->validateMethod($requestMethod);
79 11
        $this->requestMethod = $requestMethod;
80
81 11
        if ($this->shouldSetHost()) {
82 10
            $this->headers->set('Host', $this->uri->getHost());
83 10
        }
84 11
    }
85
}
86