Completed
Push — master ( d1b5d6...d751a6 )
by Sebastian
04:20
created

Request::getHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Kartenmacherei\RestFramework\Request;
3
4
use Kartenmacherei\RestFramework\Request\Body\Body;
5
use Kartenmacherei\RestFramework\Request\Header\Header;
6
use Kartenmacherei\RestFramework\Request\Header\HeaderCollection;
7
use Kartenmacherei\RestFramework\Request\Method\AbstractRequestMethod;
8
use Kartenmacherei\RestFramework\Request\Method\DeleteRequestMethod;
9
use Kartenmacherei\RestFramework\Request\Method\GetRequestMethod;
10
use Kartenmacherei\RestFramework\Request\Method\OptionsRequestMethod;
11
use Kartenmacherei\RestFramework\Request\Method\PatchRequestMethod;
12
use Kartenmacherei\RestFramework\Request\Method\PostRequestMethod;
13
use Kartenmacherei\RestFramework\Request\Method\PutRequestMethod;
14
use Kartenmacherei\RestFramework\Request\Method\RequestMethod;
15
use Kartenmacherei\RestFramework\Request\Method\UnsupportedRequestMethodException;
16
use Kartenmacherei\RestFramework\Request\UploadedFile\UploadedFilesCollection;
17
use Kartenmacherei\RestFramework\ResourceRequest\BadRequestException;
18
use Kartenmacherei\RestFramework\Token;
19
20
class Request
21
{
22
    const AUTHORIZATION_HEADER_NAME = 'HTTP_AUTHORIZATION';
23
24
    /**
25
     * @var AbstractRequestMethod
26
     */
27
    private $requestMethod;
28
29
    /**
30
     * @var Uri
31
     */
32
    private $uri;
33
34
    /**
35
     * @var Body
36
     */
37
    private $body;
38
    /**
39
     * @var HeaderCollection
40
     */
41
    private $headers;
42
43
    /**
44
     * @var UploadedFilesCollection
45
     */
46
    private $uploadedFiles;
47
48
    /**
49
     * @param AbstractRequestMethod $requestMethod
50
     * @param Uri $uri
51
     * @param Body $body
52
     * @param HeaderCollection $headers
53
     * @param UploadedFilesCollection $uploadedFiles
54
     */
55
    public function __construct(
56
        AbstractRequestMethod $requestMethod,
57
        Uri $uri,
58
        Body $body,
59
        HeaderCollection $headers,
60
        UploadedFilesCollection $uploadedFiles
61
    )
62
    {
63
        $this->requestMethod = $requestMethod;
64
        $this->uri = $uri;
65
        $this->body = $body;
66
        $this->headers = $headers;
67
        $this->uploadedFiles = $uploadedFiles;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function isOptionsRequest(): bool
74
    {
75
        return $this->requestMethod->isOptionsMethod();
76
    }
77
78
    /**
79
     * @return Request
80
     * @throws UnsupportedRequestMethodException
81
     */
82
    public static function fromSuperGlobals(): Request
83
    {
84
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
85
        $uri = new Uri($_SERVER['REQUEST_URI']);
86
87
        $body = Body::fromSuperGlobals();
88
        $headers = HeaderCollection::fromSuperGlobals();
89
        $uploadedFiles = UploadedFilesCollection::fromSuperGlobals();
90
91
        switch ($method) {
92
            case RequestMethod::OPTIONS:
93
                return new self(new OptionsRequestMethod(), $uri, $body, $headers, $uploadedFiles);
94
            case RequestMethod::DELETE:
95
                return new self(new DeleteRequestMethod(), $uri, $body, $headers, $uploadedFiles);
96
            case RequestMethod::GET:
97
                return new self(new GetRequestMethod(), $uri, $body, $headers, $uploadedFiles);
98
            case RequestMethod::PATCH:
99
                return new self(new PatchRequestMethod(), $uri, $body, $headers, $uploadedFiles);
100
            case RequestMethod::POST:
101
                return new self(new PostRequestMethod(), $uri, $body, $headers, $uploadedFiles);
102
            case RequestMethod::PUT:
103
                return new self(new PutRequestMethod(), $uri, $body, $headers, $uploadedFiles);
104
        }
105
106
        throw new UnsupportedRequestMethodException(
107
            sprintf('Unsupported method %s', $method)
108
        );
109
    }
110
111
    /**
112
     * @return Body
113
     */
114
    public function getBody(): Body
115
    {
116
        return $this->body;
117
    }
118
119
    /**
120
     * @return Uri
121
     */
122
    public function getUri(): Uri
123
    {
124
        return $this->uri;
125
    }
126
127
    /**
128
     * @return RequestMethod
129
     */
130
    public function getMethod(): RequestMethod
131
    {
132
        return $this->requestMethod;
133
    }
134
135
    /**
136
     * @return UploadedFilesCollection
137
     */
138
    public function getUploadedFiles(): UploadedFilesCollection
139
    {
140
        return $this->uploadedFiles;
141
    }
142
143
    /**
144
     * @param string $name
145
     * @return bool
146
     */
147
    private function hasHeader(string $name): bool
148
    {
149
        return $this->headers->has($name);
150
    }
151
152
    /**
153
     * @param string $name
154
     * @return Header
155
     */
156
    private function getHeader(string $name): Header
157
    {
158
        return $this->headers->get($name);
159
    }
160
161
    /**
162
     * @return Token
163
     * @throws BadRequestException
164
     */
165
    public function getAuthorizationToken(): Token
166
    {
167
        if (!$this->hasHeader(self::AUTHORIZATION_HEADER_NAME)) {
168
            throw new BadRequestException('Missing Authorization Header');
169
        }
170
        $headerValue = $this->getHeader(self::AUTHORIZATION_HEADER_NAME)->getValue();
171
        if (!preg_match('/^Bearer\s(.*)/', $headerValue, $matches)) {
172
            throw new BadRequestException('Invalid Authorization Header Value');
173
        }
174
        return new Token($matches[1]);
175
    }
176
}
177