WebApiSerializer::removeRelations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the FiveLab Resource package
7
 *
8
 * (c) FiveLab
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace FiveLab\Component\Resource\Serializers\WebApi;
15
16
use FiveLab\Component\Resource\Resource\ResourceInterface;
17
use FiveLab\Component\Resource\Serializer\Context\ResourceSerializationContext;
18
use FiveLab\Component\Resource\Serializer\ResourceSerializerInterface;
19
use FiveLab\Component\Resource\Serializer\SerializerInterface;
20
21
/**
22
 * Default Web API serializer.
23
 *
24
 * @author Vitaliy Zhuk <[email protected]>
25
 */
26
class WebApiSerializer implements ResourceSerializerInterface
27
{
28
    /**
29
     * @var SerializerInterface
30
     */
31
    private $serializer;
32
33
    /**
34
     * @var string
35
     */
36
    private $format;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param SerializerInterface $serializer
42
     * @param string              $format
43
     */
44 2
    public function __construct(SerializerInterface $serializer, string $format)
45
    {
46 2
        $this->serializer = $serializer;
47 2
        $this->format = $format;
48 2
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1 View Code Duplication
    public function serialize(ResourceInterface $resource, ResourceSerializationContext $context): string
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...
54
    {
55
        $innerContext = [
56
            'after_normalization' => function (array $data) {
57 1
                return $this->removeRelations($data);
58 1
            },
59
        ];
60
61 1
        return $this->serializer->serialize($resource, $this->format, $innerContext);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function deserialize(string $data, string $resourceClass, ResourceSerializationContext $context): ResourceInterface
68
    {
69 1
        return $this->serializer->deserialize($data, $resourceClass, $this->format);
70
    }
71
72
    /**
73
     * Remove resources from data
74
     *
75
     * @param array $data
76
     *
77
     * @return array
78
     */
79 1
    protected function removeRelations(array $data): array
80
    {
81 1
        unset($data['relations'], $data['actions']);
82
83 1
        return $data;
84
    }
85
}
86