1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiHttp\Manager; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface; |
8
|
|
|
use Chubbyphp\Deserialization\DeserializerInterface; |
9
|
|
|
use Chubbyphp\Negotiation\AcceptLanguageNegotiatorInterface; |
10
|
|
|
use Chubbyphp\Negotiation\AcceptNegotiatorInterface; |
11
|
|
|
use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface; |
12
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
13
|
|
|
|
14
|
|
|
final class RequestManager implements RequestManagerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var AcceptNegotiatorInterface |
18
|
|
|
*/ |
19
|
|
|
private $acceptNegotiator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var AcceptLanguageNegotiatorInterface |
23
|
|
|
*/ |
24
|
|
|
private $acceptLanguageNegotiator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ContentTypeNegotiatorInterface |
28
|
|
|
*/ |
29
|
|
|
private $contentTypeNegotiator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var DeserializerInterface |
33
|
|
|
*/ |
34
|
|
|
private $deserializer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param AcceptNegotiatorInterface $acceptNegotiator |
38
|
|
|
* @param AcceptLanguageNegotiatorInterface $acceptLanguageNegotiator |
39
|
|
|
* @param ContentTypeNegotiatorInterface $contentTypeNegotiator |
40
|
|
|
* @param DeserializerInterface $deserializer |
41
|
|
|
*/ |
42
|
11 |
|
public function __construct( |
43
|
|
|
AcceptNegotiatorInterface $acceptNegotiator, |
44
|
|
|
AcceptLanguageNegotiatorInterface $acceptLanguageNegotiator, |
45
|
|
|
ContentTypeNegotiatorInterface $contentTypeNegotiator, |
46
|
|
|
DeserializerInterface $deserializer |
47
|
|
|
) { |
48
|
11 |
|
$this->acceptNegotiator = $acceptNegotiator; |
49
|
11 |
|
$this->acceptLanguageNegotiator = $acceptLanguageNegotiator; |
50
|
11 |
|
$this->contentTypeNegotiator = $contentTypeNegotiator; |
51
|
11 |
|
$this->deserializer = $deserializer; |
52
|
11 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $request |
56
|
|
|
* @param string|null $default |
57
|
|
|
* |
58
|
|
|
* @return string|null |
59
|
|
|
*/ |
60
|
3 |
View Code Duplication |
public function getAccept(Request $request, string $default = null) |
|
|
|
|
61
|
|
|
{ |
62
|
3 |
|
if (null === $value = $this->acceptNegotiator->negotiate($request)) { |
63
|
2 |
|
return $default; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return $value->getValue(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Request $request |
71
|
|
|
* @param string|null $default |
72
|
|
|
* |
73
|
|
|
* @return string|null |
74
|
|
|
*/ |
75
|
3 |
View Code Duplication |
public function getAcceptLanguage(Request $request, string $default = null) |
|
|
|
|
76
|
|
|
{ |
77
|
3 |
|
if (null === $value = $this->acceptLanguageNegotiator->negotiate($request)) { |
78
|
2 |
|
return $default; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return $value->getValue(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Request $request |
86
|
|
|
* @param string|null $default |
87
|
|
|
* |
88
|
|
|
* @return string|null |
89
|
|
|
*/ |
90
|
3 |
View Code Duplication |
public function getContentType(Request $request, string $default = null) |
|
|
|
|
91
|
|
|
{ |
92
|
3 |
|
if (null === $value = $this->contentTypeNegotiator->negotiate($request)) { |
93
|
2 |
|
return $default; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return $value->getValue(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Request $request |
101
|
|
|
* @param object|string $object |
102
|
|
|
* @param string $contentType |
103
|
|
|
* @param DenormalizerContextInterface $context |
104
|
|
|
* |
105
|
|
|
* @return object |
106
|
|
|
*/ |
107
|
1 |
|
public function getDataFromRequestBody( |
108
|
|
|
Request $request, |
109
|
|
|
$object, |
110
|
|
|
string $contentType, |
111
|
|
|
DenormalizerContextInterface $context = null |
112
|
|
|
) { |
113
|
1 |
|
return $this->deserializer->deserialize($object, (string) $request->getBody(), $contentType, $context); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Request $request |
118
|
|
|
* @param object|string $object |
119
|
|
|
* @param DenormalizerContextInterface $context |
120
|
|
|
* |
121
|
|
|
* @return object |
122
|
|
|
*/ |
123
|
1 |
|
public function getDataFromRequestQuery(Request $request, $object, DenormalizerContextInterface $context = null) |
124
|
|
|
{ |
125
|
1 |
|
return $this->deserializer->denormalize($object, $request->getQueryParams(), $context); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.