Completed
Push — master ( c6c53c...f2c602 )
by Sebastian
02:05
created

Request::getAuthorizationToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
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\RequestMethod;
8
use Kartenmacherei\RestFramework\Request\Method\UnsupportedRequestMethodException;
9
use Kartenmacherei\RestFramework\Request\UploadedFile\UploadedFilesCollection;
10
use Kartenmacherei\RestFramework\ResourceRequest\BadRequestException;
11
use Kartenmacherei\RestFramework\Token;
12
13
abstract class Request
14
{
15
    const AUTHORIZATION_HEADER_NAME = 'HTTP_AUTHORIZATION';
16
17
    /**
18
     * @var Uri
19
     */
20
    private $uri;
21
22
    /**
23
     * @var HeaderCollection
24
     */
25
    private $headers;
26
27
    /**
28
     * @param Uri $uri
29
     * @param HeaderCollection $headers
30
     */
31
    public function __construct(Uri $uri, HeaderCollection $headers)
32
    {
33
        $this->uri = $uri;
34
        $this->headers = $headers;
35
    }
36
37
    /**
38
     * @return bool
39
     */
40
    public function isOptionsRequest(): bool
41
    {
42
        return false;
43
    }
44
45
    /**
46
     * @return RequestMethod
47
     */
48
    abstract public function getMethod(): RequestMethod;
49
50
    /**
51
     * @return Request
52
     * @throws UnsupportedRequestMethodException
53
     */
54
    public static function fromSuperGlobals(): Request
55
    {
56
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
57
        $uri = new Uri($_SERVER['REQUEST_URI']);
58
59
        $body = Body::fromSuperGlobals();
60
        $headers = HeaderCollection::fromSuperGlobals();
61
        $uploadedFiles = UploadedFilesCollection::fromSuperGlobals();
62
63
        switch ($method) {
64
            case RequestMethod::OPTIONS:
65
                return new OptionsRequest($uri, $headers);
66
            case RequestMethod::DELETE:
67
                return new DeleteRequest($uri, $headers, $_GET, $body);
68
            case RequestMethod::GET:
69
                return new GetRequest($uri, $headers, $_GET);
70
            case RequestMethod::PATCH:
71
                return new PatchRequest($uri, $headers, $body, $uploadedFiles);
72
            case RequestMethod::POST:
73
                return new PostRequest($uri, $headers, $body, $uploadedFiles);
74
            case RequestMethod::PUT:
75
                return new PutRequest($uri, $headers, $body, $uploadedFiles);
76
        }
77
78
        throw new UnsupportedRequestMethodException(sprintf('Unsupported method %s', $method));
79
    }
80
81
    /**
82
     * @return Uri
83
     */
84
    public function getUri(): Uri
85
    {
86
        return $this->uri;
87
    }
88
89
    /**
90
     * @param string $name
91
     * @return bool
92
     */
93
    public function hasHeader(string $name): bool
94
    {
95
        return $this->headers->has($name);
96
    }
97
98
    /**
99
     * @param string $name
100
     * @return Header
101
     */
102
    public function getHeader(string $name): Header
103
    {
104
        return $this->headers->get($name);
105
    }
106
107
    /**
108
     * @return Token
109
     * @throws BadRequestException
110
     */
111
    public function getAuthorizationToken(): Token
112
    {
113
        if (!$this->hasHeader(self::AUTHORIZATION_HEADER_NAME)) {
114
            throw new BadRequestException('Missing Authorization Header');
115
        }
116
        $headerValue = $this->getHeader(self::AUTHORIZATION_HEADER_NAME)->getValue();
117
        if (!preg_match('/^Bearer\s(.*)/', $headerValue, $matches)) {
118
            throw new BadRequestException('Invalid Authorization Header Value');
119
        }
120
        return new Token($matches[1]);
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    public function hasBody(): bool
127
    {
128
        return !$this instanceof GetRequest && !$this instanceof OptionsRequest;
129
    }
130
}
131