Passed
Push — master ( 0b922d...4a0c9c )
by Dominik
03:24
created

RequestManager::getSerializerMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
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 18
    public function __construct(
50
        AcceptNegotiatorInterface $acceptNegotiator,
51
        AcceptLanguageNegotiatorInterface $acceptLanguageNegotiator,
52
        ContentTypeNegotiatorInterface $contentTypeNegotiator,
53
        DeserializerInterface $deserializer,
54
        TransformerInterface $transformer
55
    ) {
56 18
        $this->acceptNegotiator = $acceptNegotiator;
57 18
        $this->acceptLanguageNegotiator = $acceptLanguageNegotiator;
58 18
        $this->contentTypeNegotiator = $contentTypeNegotiator;
59 18
        $this->deserializer = $deserializer;
60 18
        $this->transformer = $transformer;
61 18
    }
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 5 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 5
        if (null === $value = $this->contentTypeNegotiator->negotiate($request)) {
102 3
            return $default;
103
        }
104
105 2
        return $value->getValue();
106
    }
107
108
    /**
109
     * @param Request       $request
110
     * @param object|string $object
111
     * @param string        $contentType
112
     *
113
     * @return object|null
114
     */
115 2
    public function getDataFromRequestBody(Request $request, $object, string $contentType)
116
    {
117
        try {
118 2
            $data = $this->transformer->transform((string) $request->getBody(), $contentType);
119 1
        } catch (TransformerException $e) {
120 1
            return null;
121
        }
122
123 1
        $method = $this->getSerializerMethod($object);
124
125 1
        return $this->deserializer->$method($data, $object);
126
    }
127
128
    /**
129
     * @param Request       $request
130
     * @param object|string $object
131
     *
132
     * @return object|null
133
     */
134 1
    public function getDataFromRequestQuery(Request $request, $object)
135
    {
136 1
        $method = $this->getSerializerMethod($object);
137
138 1
        return $this->deserializer->$method($request->getQueryParams(), $object);
139
    }
140
141
    /**
142
     * @return string[]
143
     */
144
    public function getSupportedAccepts(): array
145
    {
146
        return $this->acceptNegotiator->getSupportedMediaTypes();
147
    }
148
149
    /**
150
     * @return string[]
151
     */
152
    public function getSupportedContentTypes(): array
153
    {
154
        return $this->contentTypeNegotiator->getSupportedMediaTypes();
155
    }
156
157
    /**
158
     * @return string[]
159
     */
160
    public function getSupportedLocales(): array
161
    {
162
        return $this->acceptLanguageNegotiator->getSupportedLocales();
163
    }
164
165
    /**
166
     * @param object|string $object
167
     *
168
     * @return string
169
     */
170 2
    private function getSerializerMethod($object): string
171
    {
172 2
        return is_string($object) ? 'deserializeByClass' : 'deserializeByObject';
173
    }
174
}
175