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\ResourceRequest\BadRequestException; |
17
|
|
|
use Kartenmacherei\RestFramework\Token; |
18
|
|
|
|
19
|
|
|
class Request |
20
|
|
|
{ |
21
|
|
|
const AUTHORIZATION_HEADER_NAME = 'HTTP_AUTHORIZATION'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var AbstractRequestMethod |
25
|
|
|
*/ |
26
|
|
|
private $requestMethod; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Uri |
30
|
|
|
*/ |
31
|
|
|
private $uri; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Body |
35
|
|
|
*/ |
36
|
|
|
private $body; |
37
|
|
|
/** |
38
|
|
|
* @var HeaderCollection |
39
|
|
|
*/ |
40
|
|
|
private $headers; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param AbstractRequestMethod $requestMethod |
44
|
|
|
* @param Uri $uri |
45
|
|
|
* @param Body $body |
46
|
|
|
* @param HeaderCollection $headers |
47
|
|
|
*/ |
48
|
|
|
public function __construct(AbstractRequestMethod $requestMethod, Uri $uri, Body $body, HeaderCollection $headers) |
49
|
|
|
{ |
50
|
|
|
$this->requestMethod = $requestMethod; |
51
|
|
|
$this->uri = $uri; |
52
|
|
|
$this->body = $body; |
53
|
|
|
$this->headers = $headers; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Request |
58
|
|
|
* @throws UnsupportedRequestMethodException |
59
|
|
|
*/ |
60
|
|
|
public static function fromSuperGlobals(): Request |
61
|
|
|
{ |
62
|
|
|
$method = strtoupper($_SERVER['REQUEST_METHOD']); |
63
|
|
|
$uri = new Uri($_SERVER['REQUEST_URI']); |
64
|
|
|
|
65
|
|
|
$body = Body::fromSuperGlobals(); |
66
|
|
|
$headers = HeaderCollection::fromSuperGlobals(); |
67
|
|
|
|
68
|
|
|
switch ($method) { |
69
|
|
|
case RequestMethod::OPTIONS: |
70
|
|
|
return new self(new OptionsRequestMethod(), $uri, $body, $headers); |
71
|
|
|
case RequestMethod::DELETE: |
72
|
|
|
return new self(new DeleteRequestMethod(), $uri, $body, $headers); |
73
|
|
|
case RequestMethod::GET: |
74
|
|
|
return new self(new GetRequestMethod(), $uri, $body, $headers); |
75
|
|
|
case RequestMethod::PATCH: |
76
|
|
|
return new self(new PatchRequestMethod(), $uri, $body, $headers); |
77
|
|
|
case RequestMethod::POST: |
78
|
|
|
return new self(new PostRequestMethod(), $uri, $body, $headers); |
79
|
|
|
case RequestMethod::PUT: |
80
|
|
|
return new self(new PutRequestMethod(), $uri, $body, $headers); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new UnsupportedRequestMethodException( |
84
|
|
|
sprintf('Unsupported method %s', $method) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Body |
90
|
|
|
*/ |
91
|
|
|
public function getBody(): Body |
92
|
|
|
{ |
93
|
|
|
return $this->body; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Uri |
98
|
|
|
*/ |
99
|
|
|
public function getUri(): Uri |
100
|
|
|
{ |
101
|
|
|
return $this->uri; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return RequestMethod |
106
|
|
|
*/ |
107
|
|
|
public function getMethod(): RequestMethod |
108
|
|
|
{ |
109
|
|
|
return $this->requestMethod; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $name |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
private function hasHeader(string $name): bool |
117
|
|
|
{ |
118
|
|
|
return $this->headers->has($name); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $name |
123
|
|
|
* @return Header |
124
|
|
|
*/ |
125
|
|
|
private function getHeader(string $name): Header |
126
|
|
|
{ |
127
|
|
|
return $this->headers->get($name); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return Token |
132
|
|
|
* @throws BadRequestException |
133
|
|
|
*/ |
134
|
|
|
public function getAuthorizationToken(): Token |
135
|
|
|
{ |
136
|
|
|
if (!$this->hasHeader(self::AUTHORIZATION_HEADER_NAME)) { |
137
|
|
|
throw new BadRequestException('Missing Authorization Header'); |
138
|
|
|
} |
139
|
|
|
$headerValue = $this->getHeader(self::AUTHORIZATION_HEADER_NAME)->getValue(); |
140
|
|
|
if (!preg_match('/^Bearer\s(.*)/', $headerValue, $matches)) { |
141
|
|
|
throw new BadRequestException('Invalid Authorization Header Value'); |
142
|
|
|
} |
143
|
|
|
return new Token($matches[1]); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|