Passed
Push — master ( 675d65...f3d3c0 )
by Dominik
05:31
created

AcceptAndContentTypeMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 27.14 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 19
loc 70
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A process() 19 31 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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