Completed
Push — master ( 6e9e2e...a59fd9 )
by Dominik
02:04
created

RequestManager::getAccept()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
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 3
    public function __construct(
43
        AcceptNegotiatorInterface $acceptNegotiator,
44
        AcceptLanguageNegotiatorInterface $acceptLanguageNegotiator,
45
        ContentTypeNegotiatorInterface $contentTypeNegotiator,
46
        DeserializerInterface $deserializer
47
    ) {
48 3
        $this->acceptNegotiator = $acceptNegotiator;
49 3
        $this->acceptLanguageNegotiator = $acceptLanguageNegotiator;
50 3
        $this->contentTypeNegotiator = $contentTypeNegotiator;
51 3
        $this->deserializer = $deserializer;
52 3
    }
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)
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...
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 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...
76
    {
77
        if (null === $value = $this->acceptLanguageNegotiator->negotiate($request)) {
78
            return $default;
79
        }
80
81
        return $value->getValue();
82
    }
83
84
    /**
85
     * @param Request     $request
86
     * @param string|null $default
87
     *
88
     * @return string|null
89
     */
90 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...
91
    {
92
        if (null === $value = $this->contentTypeNegotiator->negotiate($request)) {
93
            return $default;
94
        }
95
96
        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
    public function getDataFromRequestBody(
108
        Request $request,
109
        $object,
110
        string $contentType,
111
        DenormalizerContextInterface $context = null
112
    ) {
113
        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
    public function getDataFromRequestQuery(Request $request, $object, DenormalizerContextInterface $context = null)
124
    {
125
        return $this->deserializer->denormalize($object, $request->getQueryParams(), $context);
126
    }
127
}
128