Passed
Branch 3.0.0 (0ebb76)
by Pieter
02:27
created

SymfonySerializerAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A postData() 0 9 1
A normalize() 0 4 1
A __construct() 0 4 1
A putData() 0 10 1
A toResponse() 0 7 2
1
<?php
2
3
namespace W2w\Lib\Apie\Plugins\Core\Serializers;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Symfony\Component\Serializer\Serializer;
7
use W2w\Lib\Apie\Interfaces\FormatRetrieverInterface;
8
use W2w\Lib\Apie\Interfaces\ResourceSerializerInterface;
9
use Zend\Diactoros\Response\TextResponse;
10
11
class SymfonySerializerAdapter implements ResourceSerializerInterface
12
{
13
    private $serializer;
14
15
    private $formatRetriever;
16
17
    public function __construct(Serializer $serializer, FormatRetrieverInterface $formatRetriever)
18
    {
19
        $this->serializer = $serializer;
20
        $this->formatRetriever = $formatRetriever;
21
    }
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function putData(object $resource, string $requestBody, string $contentType): object
27
    {
28
        $contentFormat = $this->formatRetriever->getFormat($contentType);
29
        return $this->serializer->deserialize(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->serializer...opulate' => $resource)) could return the type array which is incompatible with the type-hinted return object. Consider adding an additional type-check to rule them out.
Loading history...
30
            $requestBody,
31
            get_class($resource),
32
            $contentFormat,
0 ignored issues
show
Bug introduced by
It seems like $contentFormat can also be of type null; however, parameter $format of Symfony\Component\Serial...rializer::deserialize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
            /** @scrutinizer ignore-type */ $contentFormat,
Loading history...
33
            [
34
                'groups' => ['base', 'write', 'put'],
35
                'object_to_populate' => $resource
36
            ]
37
        );
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function postData(string $resourceClass, string $requestBody, string $contentType): object
44
    {
45
        $contentFormat = $this->formatRetriever->getFormat($contentType);
46
        return $this->serializer->deserialize(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->serializer...se', 'write', 'post'))) could return the type array which is incompatible with the type-hinted return object. Consider adding an additional type-check to rule them out.
Loading history...
47
            $requestBody,
48
            $resourceClass,
49
            $contentFormat,
0 ignored issues
show
Bug introduced by
It seems like $contentFormat can also be of type null; however, parameter $format of Symfony\Component\Serial...rializer::deserialize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            /** @scrutinizer ignore-type */ $contentFormat,
Loading history...
50
            [
51
                'groups' => ['base', 'write', 'post'],
52
            ]
53
        );
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function toResponse(?object $resource, string $acceptHeader): ResponseInterface
60
    {
61
        $format = $this->formatRetriever->getFormat($acceptHeader ?? 'application/json');
62
        $contentType = $this->formatRetriever->getContentType($format);
0 ignored issues
show
Bug introduced by
It seems like $format can also be of type null; however, parameter $format of W2w\Lib\Apie\Interfaces\...rface::getContentType() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        $contentType = $this->formatRetriever->getContentType(/** @scrutinizer ignore-type */ $format);
Loading history...
63
        $response = $this->serializer->serialize($resource, $format, ['groups' => ['base', 'read', 'get']]);
0 ignored issues
show
Bug introduced by
It seems like $format can also be of type null; however, parameter $format of Symfony\Component\Serial...Serializer::serialize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        $response = $this->serializer->serialize($resource, /** @scrutinizer ignore-type */ $format, ['groups' => ['base', 'read', 'get']]);
Loading history...
64
65
        return new TextResponse($response, is_null($resource) ? 204 : 200, ['content-type' => $contentType]);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function normalize(?object $resource, string $acceptHeader)
72
    {
73
        $format = $this->formatRetriever->getFormat($acceptHeader ?? 'application/json');
74
        return $this->serializer->normalize($resource, $format, ['groups' => ['base', 'read', 'get']]);
75
    }
76
}
77