1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the reva2/jsonapi. |
4
|
|
|
* |
5
|
|
|
* (c) Sergey Revenko <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Reva2\JsonApi\Http\Headers; |
12
|
|
|
|
13
|
|
|
use Neomerx\JsonApi\Contracts\Codec\CodecMatcherInterface; |
14
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\AcceptHeaderInterface; |
15
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\HeaderInterface; |
16
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\HeaderParametersInterface; |
17
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\HeadersCheckerInterface; |
18
|
|
|
use Neomerx\JsonApi\Document\Error; |
19
|
|
|
use Neomerx\JsonApi\Exceptions\JsonApiException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Headers checker |
23
|
|
|
* |
24
|
|
|
* @package Reva2\JsonApi\Http\Headers |
25
|
|
|
* @author Sergey Revenko <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class HeadersChecker implements HeadersCheckerInterface |
28
|
|
|
{ |
29
|
|
|
const INVALID_CONTENT_TYPE_ERROR = 'ebb239c5-221d-42d2-a2f7-ca4bbc3d476f'; |
30
|
|
|
const UNSUPPORTED_CONTENT_TYPE_ERROR = 'aa389b54-1b33-4b0f-a5f4-1d680ca5f6d3'; |
31
|
|
|
const UNSUPPORTED_ACCEPT_ERROR = '6df3b139-d9ef-4576-8d0a-c0ce2e803827'; |
32
|
|
|
/** |
33
|
|
|
* @var CodecMatcherInterface |
34
|
|
|
*/ |
35
|
|
|
protected $matcher; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor |
39
|
|
|
* |
40
|
|
|
* @param CodecMatcherInterface $matcher |
41
|
|
|
*/ |
42
|
5 |
|
public function __construct(CodecMatcherInterface $matcher) |
43
|
|
|
{ |
44
|
5 |
|
$this->matcher = $matcher; |
45
|
5 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
4 |
|
public function checkHeaders(HeaderParametersInterface $parameters) |
51
|
|
|
{ |
52
|
|
|
|
53
|
4 |
|
$this->checkContentTypeHeader($parameters->getContentTypeHeader()); |
54
|
2 |
|
$this->checkAcceptHeader($parameters->getAcceptHeader()); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check content type header |
59
|
|
|
* |
60
|
|
|
* @param HeaderInterface $header |
61
|
|
|
*/ |
62
|
4 |
|
private function checkContentTypeHeader(HeaderInterface $header) |
63
|
|
|
{ |
64
|
4 |
|
if (count($header->getMediaTypes()) > 1) { |
65
|
1 |
|
throw new JsonApiException( |
66
|
1 |
|
new Error( |
67
|
1 |
|
rand(), |
68
|
1 |
|
null, |
69
|
1 |
|
JsonApiException::HTTP_CODE_BAD_REQUEST, |
70
|
1 |
|
self::INVALID_CONTENT_TYPE_ERROR, |
71
|
|
|
"Invalid content type" |
72
|
1 |
|
), |
73
|
|
|
JsonApiException::HTTP_CODE_BAD_REQUEST |
74
|
1 |
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
$this->matcher->matchDecoder($header); |
78
|
3 |
View Code Duplication |
if (null === $this->matcher->getDecoderHeaderMatchedType()) { |
|
|
|
|
79
|
1 |
|
throw new JsonApiException( |
80
|
1 |
|
new Error( |
81
|
1 |
|
rand(), |
82
|
1 |
|
null, |
83
|
1 |
|
JsonApiException::HTTP_CODE_UNSUPPORTED_MEDIA_TYPE, |
84
|
1 |
|
self::UNSUPPORTED_CONTENT_TYPE_ERROR, |
85
|
|
|
'Unsupported content type' |
86
|
1 |
|
), |
87
|
|
|
JsonApiException::HTTP_CODE_UNSUPPORTED_MEDIA_TYPE |
88
|
1 |
|
); |
89
|
|
|
} |
90
|
2 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Check accept header |
94
|
|
|
* |
95
|
|
|
* @param AcceptHeaderInterface $header |
96
|
|
|
* @return Error |
97
|
|
|
*/ |
98
|
2 |
|
private function checkAcceptHeader(AcceptHeaderInterface $header) |
99
|
|
|
{ |
100
|
2 |
|
$this->matcher->matchEncoder($header); |
101
|
|
|
|
102
|
2 |
View Code Duplication |
if (null === $this->matcher->getEncoderHeaderMatchedType()) { |
|
|
|
|
103
|
1 |
|
throw new JsonApiException( |
104
|
1 |
|
new Error( |
105
|
1 |
|
rand(), |
106
|
1 |
|
null, |
107
|
1 |
|
JsonApiException::HTTP_CODE_UNSUPPORTED_MEDIA_TYPE, |
108
|
1 |
|
self::UNSUPPORTED_ACCEPT_ERROR, |
109
|
|
|
'Unsupported media type' |
110
|
1 |
|
), |
111
|
|
|
JsonApiException::HTTP_CODE_UNSUPPORTED_MEDIA_TYPE |
112
|
1 |
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.