Passed
Branch 3.0.0 (98e096)
by Pieter
02:47
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 putData() 0 10 1
A __construct() 0 4 1
A postData() 0 9 1
A toResponse() 0 7 2
A normalize() 0 4 1
1
<?php
2
3
4
namespace W2w\Lib\Apie\Serializers;
5
6
7
use Psr\Http\Message\ResponseInterface;
8
use Symfony\Component\Serializer\Serializer;
9
use W2w\Lib\Apie\Encodings\FormatRetrieverInterface;
10
use Zend\Diactoros\Response\TextResponse;
11
12
class SymfonySerializerAdapter implements ResourceSerializerInterface
13
{
14
    private $serializer;
15
16
    private $formatRetriever;
17
18
    public function __construct(Serializer $serializer, FormatRetrieverInterface $formatRetriever)
19
    {
20
        $this->serializer = $serializer;
21
        $this->formatRetriever = $formatRetriever;
22
    }
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function putData(object $resource, string $requestBody, string $contentType): object
28
    {
29
        $contentFormat = $this->formatRetriever->getFormat($contentType);
30
        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...
31
            $requestBody,
32
            get_class($resource),
33
            $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

33
            /** @scrutinizer ignore-type */ $contentFormat,
Loading history...
34
            [
35
                'groups' => ['base', 'write', 'put'],
36
                'object_to_populate' => $resource
37
            ]
38
        );
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function postData(string $resourceClass, string $requestBody, string $contentType): object
45
    {
46
        $contentFormat = $this->formatRetriever->getFormat($contentType);
47
        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...
48
            $requestBody,
49
            $resourceClass,
50
            $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

50
            /** @scrutinizer ignore-type */ $contentFormat,
Loading history...
51
            [
52
                'groups' => ['base', 'write', 'post'],
53
            ]
54
        );
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function toResponse(?object $resource, string $acceptHeader): ResponseInterface
61
    {
62
        $format = $this->formatRetriever->getFormat($acceptHeader ?? 'application/json');
63
        $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\Encodings\F...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

63
        $contentType = $this->formatRetriever->getContentType(/** @scrutinizer ignore-type */ $format);
Loading history...
64
        $response = $this->serializer->serialize($this->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

64
        $response = $this->serializer->serialize($this->resource, /** @scrutinizer ignore-type */ $format, ['groups' => ['base', 'read', 'get']]);
Loading history...
Bug Best Practice introduced by
The property resource does not exist on W2w\Lib\Apie\Serializers\SymfonySerializerAdapter. Did you maybe forget to declare it?
Loading history...
65
66
        return new TextResponse($response, is_null($resource) ? 204 : 200, ['content-type' => $contentType]);
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function normalize(?object $resource, string $acceptHeader)
73
    {
74
        $format = $this->formatRetriever->getFormat($acceptHeader ?? 'application/json');
75
        return $this->serializer->normalize($this->resource, $format, ['groups' => ['base', 'read', 'get']]);
0 ignored issues
show
Bug Best Practice introduced by
The property resource does not exist on W2w\Lib\Apie\Serializers\SymfonySerializerAdapter. Did you maybe forget to declare it?
Loading history...
76
    }
77
}
78