Completed
Push — master ( 9e1c2f...16e6fb )
by Bohuslav
03:00
created

Request::__construct()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 3
Ratio 12.5 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 3
loc 24
ccs 13
cts 13
cp 1
rs 8.5125
cc 6
eloc 16
nc 5
nop 5
crap 6
1
<?php
2
namespace Kambo\HttpMessage;
3
4
// \Spl
5
use InvalidArgumentException;
6
7
// \Psr
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\UriInterface;
10
11
// \HttpMessage
12
use Kambo\HttpMessage\Message;
13
use Kambo\HttpMessage\Headers;
14
use Kambo\HttpMessage\RequestTrait;
15
use Kambo\HttpMessage\Factories\String\UriFactory;
16
use Kambo\HttpMessage\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\HttpMessage
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 12
    public function __construct(
60
        $requestMethod,
61
        $uri,
62
        $headers = null,
63
        $body = null,
64
        $protocol = '1.1'
65
    ) {
66 12
        parent::__construct($headers, $body, $protocol);
67
68 11
        if (is_string($uri)) {
69 10
            $this->uri = UriFactory::create($uri);
70 11
        } elseif (!($uri instanceof UriInterface)) {
71 1
            throw new InvalidArgumentException(
72
                'URI must be a string or implement Psr\Http\Message\UriInterface'
73 1
            );
74
        }
75
76 10
        $this->validateMethod($requestMethod);
77 10
        $this->requestMethod   = $requestMethod;
78
79 10 View Code Duplication
        if ($this->uri->getHost() !== '' && (!$this->hasHeader('Host') || $this->getHeader('Host') === null)) {
80 9
            $this->headers->set('Host', $this->uri->getHost());
81 9
        }
82 10
    }
83
}
84