Request::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
1
<?php
2
3
namespace Saxulum\HttpClient;
4
5
class Request extends AbstractMessage
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $method;
11
12
    const METHOD_OPTIONS = 'OPTIONS';
13
    const METHOD_GET = 'GET';
14
    const METHOD_HEAD = 'HEAD';
15
    const METHOD_POST = 'POST';
16
    const METHOD_PUT = 'PUT';
17
    const METHOD_DELETE = 'DELETE';
18
    const METHOD_PATCH = 'PATCH';
19
20
    /**
21
     * @var Url
22
     */
23
    protected $url;
24
25
    /**
26
     * @param string      $protocolVersion
27
     * @param string      $method
28
     * @param string      $url
29
     * @param array       $headers
30
     * @param string|null $content
31
     */
32
    public function __construct(
33
        $protocolVersion,
34
        $method,
35
        $url,
36
        array $headers = array(),
37
        $content = null
38
    ) {
39
        $this->protocolVersion = $protocolVersion;
40
        $this->method = strtoupper($method);
41
        $this->url = new Url($url);
42
        $this->headers = $headers;
43
        $this->content = $content;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getMethod()
50
    {
51
        return $this->method;
52
    }
53
54
    /**
55
     * @return Url
56
     */
57
    public function getUrl()
58
    {
59
        return $this->url;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function __toString()
66
    {
67
        $request = "{$this->getMethod()} {$this->getUrl()->getResource()} HTTP/{$this->getProtocolVersion()}\r\n";
68
        $request .= "Host: {$this->getUrl()->getHost()}\r\n";
69
        foreach ($this->getHeaders() as $headerName => $headerValue) {
70
            $request .= "{$headerName}: {$headerValue}\r\n";
71
        }
72
73
        if (null !== $this->getContent()) {
74
            $request .= "\r\n{$this->getContent()}\r\n";
75
        }
76
77
        $request .= "\r\n";
78
79
        return $request;
80
    }
81
}
82