1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiHttp\Middleware; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\ApiHttp\ApiProblem\ClientError\NotAcceptable; |
8
|
|
|
use Chubbyphp\ApiHttp\ApiProblem\ClientError\UnsupportedMediaType; |
9
|
|
|
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface; |
10
|
|
|
use Chubbyphp\Negotiation\AcceptNegotiatorInterface; |
11
|
|
|
use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
15
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
16
|
|
|
|
17
|
|
|
final class AcceptAndContentTypeMiddleware implements MiddlewareInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var AcceptNegotiatorInterface |
21
|
|
|
*/ |
22
|
|
|
private $acceptNegotiator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ContentTypeNegotiatorInterface |
26
|
|
|
*/ |
27
|
|
|
private $contentTypeNegotiator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ResponseManagerInterface |
31
|
|
|
*/ |
32
|
|
|
private $responseManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param AcceptNegotiatorInterface $acceptNegotiator |
36
|
|
|
* @param ContentTypeNegotiatorInterface $contentTypeNegotiator |
37
|
|
|
* @param ResponseManagerInterface $responseManager |
38
|
|
|
*/ |
39
|
4 |
|
public function __construct( |
40
|
|
|
AcceptNegotiatorInterface $acceptNegotiator, |
41
|
|
|
ContentTypeNegotiatorInterface $contentTypeNegotiator, |
42
|
|
|
ResponseManagerInterface $responseManager |
43
|
|
|
) { |
44
|
4 |
|
$this->acceptNegotiator = $acceptNegotiator; |
45
|
4 |
|
$this->contentTypeNegotiator = $contentTypeNegotiator; |
46
|
4 |
|
$this->responseManager = $responseManager; |
47
|
4 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param ServerRequestInterface $request |
51
|
|
|
* @param RequestHandlerInterface $handler |
52
|
|
|
* |
53
|
|
|
* @return ResponseInterface |
54
|
|
|
*/ |
55
|
4 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
56
|
|
|
{ |
57
|
4 |
View Code Duplication |
if (null === $accept = $this->acceptNegotiator->negotiate($request)) { |
|
|
|
|
58
|
1 |
|
$supportedMediaTypes = $this->acceptNegotiator->getSupportedMediaTypes(); |
59
|
1 |
|
return $this->responseManager->createFromApiProblem( |
60
|
1 |
|
new NotAcceptable( |
61
|
1 |
|
$request->getHeaderLine('Accept'), |
62
|
|
|
$supportedMediaTypes |
63
|
|
|
), |
64
|
1 |
|
$supportedMediaTypes[0] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
$request = $request->withAttribute('accept', $accept->getValue()); |
69
|
|
|
|
70
|
3 |
|
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) { |
71
|
2 |
View Code Duplication |
if (null === $contentType = $this->contentTypeNegotiator->negotiate($request)) { |
|
|
|
|
72
|
1 |
|
return $this->responseManager->createFromApiProblem( |
73
|
1 |
|
new UnsupportedMediaType( |
74
|
1 |
|
$request->getHeaderLine('Content-Type'), |
75
|
1 |
|
$this->contentTypeNegotiator->getSupportedMediaTypes() |
76
|
|
|
), |
77
|
1 |
|
$accept->getValue() |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$request = $request->withAttribute('contentType', $contentType->getValue()); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return $handler->handle($request); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.