Passed
Branch master (a7861d)
by Dominik
03:10
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 Negotiation\Accept as ContentAccept;
11
use Negotiation\Negotiator as ContentNegotiator;
12
use Psr\Http\Message\ServerRequestInterface as Request;
13
14
final class RequestManager implements RequestManagerInterface
15
{
16
    /**
17
     * @var ContentNegotiator
18
     */
19
    private $contentNegotiator;
20
21
    /**
22
     * @var DeserializerInterface
23
     */
24
    private $deserializer;
25
26
    /**
27
     * @var TransformerInterface
28
     */
29
    private $transformer;
30
31
    /**
32
     * @param ContentNegotiator     $contentNegotiator
33
     * @param DeserializerInterface $deserializer
34
     * @param TransformerInterface  $transformer
35
     */
36 10
    public function __construct(
37
        ContentNegotiator $contentNegotiator,
38
        DeserializerInterface $deserializer,
39
        TransformerInterface $transformer
40
    ) {
41 10
        $this->contentNegotiator = $contentNegotiator;
42 10
        $this->deserializer = $deserializer;
43 10
        $this->transformer = $transformer;
44 10
    }
45
46
    /**
47
     * @param Request $request
48
     *
49
     * @return string|null
50
     */
51 3
    public function getAccept(Request $request)
52
    {
53 3
        return $this->negotiate($request, 'Accept');
54
    }
55
56
    /**
57
     * @param Request $request
58
     *
59
     * @return string|null
60
     */
61 6
    public function getContentType(Request $request)
62
    {
63 6
        return $this->negotiate($request, 'Content-Type');
64
    }
65
66
    /**
67
     * @param Request $request
68
     * @param string  $headerName
69
     *
70
     * @return null|string
71
     */
72 9
    private function negotiate(Request $request, string $headerName)
73
    {
74 9
        if (!$request->hasHeader($headerName)) {
75 3
            return null;
76
        }
77
78
        /** @var ContentAccept $best */
79 6
        $best = $this->contentNegotiator->getBest(
80 6
            $request->getHeaderLine($headerName),
81 6
            $this->transformer->getContentTypes()
82
        );
83
84 6
        if (null === $best) {
85 2
            return null;
86
        }
87
88 4
        return $best->getNormalizedValue();
89
    }
90
91
    /**
92
     * @param Request       $request
93
     * @param object|string $object
94
     *
95
     * @return object|null
96
     */
97 3
    public function getDataFromRequestBody(Request $request, $object)
98
    {
99 3
        if (null === $contentType = $this->getContentType($request)) {
100 1
            return null;
101
        }
102
103
        try {
104 2
            $data = $this->transformer->transform((string) $request->getBody(), $contentType);
105 1
        } catch (TransformerException $e) {
106 1
            return null;
107
        }
108
109 1
        $method = $this->getSerializerMethod($object);
110
111 1
        return $this->deserializer->$method($data, $object);
112
    }
113
114
    /**
115
     * @param Request       $request
116
     * @param object|string $object
117
     *
118
     * @return object|null
119
     */
120 1
    public function getDataFromRequestQuery(Request $request, $object)
121
    {
122 1
        $method = $this->getSerializerMethod($object);
123
124 1
        return $this->deserializer->$method($request->getQueryParams(), $object);
125
    }
126
127
    /**
128
     * @param object|string $object
129
     *
130
     * @return string
131
     */
132 2
    private function getSerializerMethod($object): string
133
    {
134 2
        return is_string($object) ? 'deserializeByClass' : 'deserializeByObject';
135
    }
136
}
137