Completed
Push — master ( ac4cc7...3c4d8e )
by Tobias
03:01
created

RequestTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 0
dl 0
loc 98
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethod() 0 4 1
A withMethod() 0 11 2
A getUri() 0 4 1
A getRequestTarget() 0 15 4
A withRequestTarget() 0 11 2
A withUri() 0 15 4
A updateHostFromUri() 0 20 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Psr7;
6
7
use Psr\Http\Message\UriInterface;
8
9
/**
10
 * @author Michael Dowling and contributors to guzzlehttp/psr7
11
 * @author Tobias Nyholm <[email protected]>
12
 * @author Martijn van der Ven <[email protected]>
13
 *
14
 * @internal should not be used outside of Nyholm/Psr7 as it does not fall under our BC promise
15
 */
16
trait RequestTrait
17
{
18
    /** @var string */
19
    private $method;
20
21
    /** @var string|null */
22
    private $requestTarget;
23
24
    /** @var UriInterface|null */
25
    private $uri;
26
27 6
    public function getRequestTarget(): string
28
    {
29 6
        if (null !== $this->requestTarget) {
30 2
            return $this->requestTarget;
31
        }
32
33 6
        if ('' === $target = $this->uri->getPath()) {
34 2
            $target = '/';
35
        }
36 6
        if ('' !== $this->uri->getQuery()) {
37 3
            $target .= '?' . $this->uri->getQuery();
38
        }
39
40 6
        return $target;
41
    }
42
43 4
    public function withRequestTarget($requestTarget): self
44
    {
45 4
        if (\preg_match('#\s#', $requestTarget)) {
46 2
            throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
47
        }
48
49 2
        $new = clone $this;
50 2
        $new->requestTarget = $requestTarget;
51
52 2
        return $new;
53
    }
54
55 27
    public function getMethod(): string
56
    {
57 27
        return $this->method;
58
    }
59
60 7
    public function withMethod($method): self
61
    {
62 7
        if (!\is_string($method)) {
63 6
            throw new \InvalidArgumentException('Method must be a string');
64
        }
65
66 1
        $new = clone $this;
67 1
        $new->method = $method;
68
69 1
        return $new;
70
    }
71
72 31
    public function getUri(): UriInterface
73
    {
74 31
        return $this->uri;
75
    }
76
77 10
    public function withUri(UriInterface $uri, $preserveHost = false): self
78
    {
79 10
        if ($uri === $this->uri) {
80 2
            return $this;
81
        }
82
83 9
        $new = clone $this;
84 9
        $new->uri = $uri;
85
86 9
        if (!$preserveHost || !$this->hasHeader('Host')) {
87 8
            $new->updateHostFromUri();
88
        }
89
90 9
        return $new;
91
    }
92
93 115
    private function updateHostFromUri(): void
94
    {
95 115
        if ('' === $host = $this->uri->getHost()) {
96 74
            return;
97
        }
98
99 47
        if (null !== ($port = $this->uri->getPort())) {
100 3
            $host .= ':' . $port;
101
        }
102
103 47
        if (isset($this->headerNames['host'])) {
104 3
            $header = $this->headerNames['host'];
105
        } else {
106 47
            $this->headerNames['host'] = $header = 'Host';
107
        }
108
109
        // Ensure Host is the first header.
110
        // See: http://tools.ietf.org/html/rfc7230#section-5.4
111 47
        $this->headers = [$header => [$host]] + $this->headers;
112 47
    }
113
}
114