Passed
Push — master ( d91f26...c841b7 )
by Dominik
03:09
created

RequestManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 140
Duplicated Lines 17.14 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 3
cbo 6
dl 24
loc 140
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getAccept() 8 8 2
A getAcceptLanguage() 8 8 2
A getContentType() 8 8 2
A getDataFromRequestBody() 0 16 3
A getDataFromRequestQuery() 0 6 1
A getSerializerMethod() 0 4 2

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\Manager;
6
7
use Chubbyphp\Deserialization\DeserializerInterface;
8
use Chubbyphp\Deserialization\Transformer\TransformerException;
9
use Chubbyphp\Deserialization\TransformerInterface;
10
use Chubbyphp\Negotiation\AcceptLanguageNegotiatorInterface;
11
use Chubbyphp\Negotiation\AcceptNegotiatorInterface;
12
use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface;
13
use Psr\Http\Message\ServerRequestInterface as Request;
14
15
final class RequestManager implements RequestManagerInterface
16
{
17
    /**
18
     * @var AcceptNegotiatorInterface
19
     */
20
    private $acceptNegotiator;
21
22
    /**
23
     * @var AcceptLanguageNegotiatorInterface
24
     */
25
    private $acceptLanguageNegotiator;
26
27
    /**
28
     * @var ContentTypeNegotiatorInterface
29
     */
30
    private $contentTypeNegotiator;
31
32
    /**
33
     * @var DeserializerInterface
34
     */
35
    private $deserializer;
36
37
    /**
38
     * @var TransformerInterface
39
     */
40
    private $transformer;
41
42
    /**
43
     * @param AcceptNegotiatorInterface         $acceptNegotiator
44
     * @param AcceptLanguageNegotiatorInterface $acceptLanguageNegotiator
45
     * @param ContentTypeNegotiatorInterface    $contentTypeNegotiator
46
     * @param DeserializerInterface             $deserializer
47
     * @param TransformerInterface              $transformer
48
     */
49 19
    public function __construct(
50
        AcceptNegotiatorInterface $acceptNegotiator,
51
        AcceptLanguageNegotiatorInterface $acceptLanguageNegotiator,
52
        ContentTypeNegotiatorInterface $contentTypeNegotiator,
53
        DeserializerInterface $deserializer,
54
        TransformerInterface $transformer
55
    ) {
56 19
        $this->acceptNegotiator = $acceptNegotiator;
57 19
        $this->acceptLanguageNegotiator = $acceptLanguageNegotiator;
58 19
        $this->contentTypeNegotiator = $contentTypeNegotiator;
59 19
        $this->deserializer = $deserializer;
60 19
        $this->transformer = $transformer;
61 19
    }
62
63
    /**
64
     * @param Request     $request
65
     * @param string|null $default
66
     *
67
     * @return string|null
68
     */
69 5 View Code Duplication
    public function getAccept(Request $request, string $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
70
    {
71 5
        if (null === $value = $this->acceptNegotiator->negotiate($request)) {
72 4
            return $default;
73
        }
74
75 1
        return $value->getValue();
76
    }
77
78
    /**
79
     * @param Request     $request
80
     * @param string|null $default
81
     *
82
     * @return string|null
83
     */
84 5 View Code Duplication
    public function getAcceptLanguage(Request $request, string $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
85
    {
86 5
        if (null === $value = $this->acceptLanguageNegotiator->negotiate($request)) {
87 4
            return $default;
88
        }
89
90 1
        return $value->getValue();
91
    }
92
93
    /**
94
     * @param Request     $request
95
     * @param string|null $default
96
     *
97
     * @return string|null
98
     */
99 8 View Code Duplication
    public function getContentType(Request $request, string $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
100
    {
101 8
        if (null === $value = $this->contentTypeNegotiator->negotiate($request)) {
102 4
            return $default;
103
        }
104
105 4
        return $value->getValue();
106
    }
107
108
    /**
109
     * @param Request       $request
110
     * @param object|string $object
111
     * @param string|null   $defaultContentType
112
     *
113
     * @return object|null
114
     */
115 3
    public function getDataFromRequestBody(Request $request, $object, string $defaultContentType = null)
116
    {
117 3
        if (null === $contentType = $this->getContentType($request, $defaultContentType)) {
118 1
            return null;
119
        }
120
121
        try {
122 2
            $data = $this->transformer->transform((string) $request->getBody(), $contentType);
123 1
        } catch (TransformerException $e) {
124 1
            return null;
125
        }
126
127 1
        $method = $this->getSerializerMethod($object);
128
129 1
        return $this->deserializer->$method($data, $object);
130
    }
131
132
    /**
133
     * @param Request       $request
134
     * @param object|string $object
135
     *
136
     * @return object|null
137
     */
138 1
    public function getDataFromRequestQuery(Request $request, $object)
139
    {
140 1
        $method = $this->getSerializerMethod($object);
141
142 1
        return $this->deserializer->$method($request->getQueryParams(), $object);
143
    }
144
145
    /**
146
     * @param object|string $object
147
     *
148
     * @return string
149
     */
150 2
    private function getSerializerMethod($object): string
151
    {
152 2
        return is_string($object) ? 'deserializeByClass' : 'deserializeByObject';
153
    }
154
}
155