Completed
Push — master ( ae2b3a...9c69f7 )
by Sebastian
03:09
created

Request::isOptionsRequest()   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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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 bool
58
     */
59
    public function isOptionsRequest(): bool
60
    {
61
        return $this->requestMethod->isOptionsMethod();
62
    }
63
64
    /**
65
     * @return Request
66
     * @throws UnsupportedRequestMethodException
67
     */
68
    public static function fromSuperGlobals(): Request
69
    {
70
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
71
        $uri = new Uri($_SERVER['REQUEST_URI']);
72
73
        $body = Body::fromSuperGlobals();
74
        $headers = HeaderCollection::fromSuperGlobals();
75
76
        switch ($method) {
77
            case RequestMethod::OPTIONS:
78
                return new self(new OptionsRequestMethod(), $uri, $body, $headers);
79
            case RequestMethod::DELETE:
80
                return new self(new DeleteRequestMethod(), $uri, $body, $headers);
81
            case RequestMethod::GET:
82
                return new self(new GetRequestMethod(), $uri, $body, $headers);
83
            case RequestMethod::PATCH:
84
                return new self(new PatchRequestMethod(), $uri, $body, $headers);
85
            case RequestMethod::POST:
86
                return new self(new PostRequestMethod(), $uri, $body, $headers);
87
            case RequestMethod::PUT:
88
                return new self(new PutRequestMethod(), $uri, $body, $headers);
89
        }
90
91
        throw new UnsupportedRequestMethodException(
92
            sprintf('Unsupported method %s', $method)
93
        );
94
    }
95
96
    /**
97
     * @return Body
98
     */
99
    public function getBody(): Body
100
    {
101
        return $this->body;
102
    }
103
104
    /**
105
     * @return Uri
106
     */
107
    public function getUri(): Uri
108
    {
109
        return $this->uri;
110
    }
111
112
    /**
113
     * @return RequestMethod
114
     */
115
    public function getMethod(): RequestMethod
116
    {
117
        return $this->requestMethod;
118
    }
119
120
    /**
121
     * @param string $name
122
     * @return bool
123
     */
124
    private function hasHeader(string $name): bool
125
    {
126
        return $this->headers->has($name);
127
    }
128
129
    /**
130
     * @param string $name
131
     * @return Header
132
     */
133
    private function getHeader(string $name): Header
134
    {
135
        return $this->headers->get($name);
136
    }
137
138
    /**
139
     * @return Token
140
     * @throws BadRequestException
141
     */
142
    public function getAuthorizationToken(): Token
143
    {
144
        if (!$this->hasHeader(self::AUTHORIZATION_HEADER_NAME)) {
145
            throw new BadRequestException('Missing Authorization Header');
146
        }
147
        $headerValue = $this->getHeader(self::AUTHORIZATION_HEADER_NAME)->getValue();
148
        if (!preg_match('/^Bearer\s(.*)/', $headerValue, $matches)) {
149
            throw new BadRequestException('Invalid Authorization Header Value');
150
        }
151
        return new Token($matches[1]);
152
    }
153
}
154