Passed
Push — master ( b7ffee...9640c3 )
by Dominik
02:05
created

Serializer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 41.89 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 62
loc 148
ccs 48
cts 50
cp 0.96
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B serialize() 0 26 4
A serializeFields() 20 20 3
A serializeEmbeddedFields() 20 20 3
A serializeLinks() 22 22 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization;
6
7
use Chubbyphp\Serialization\Mapping\ObjectMappingInterface;
8
use Chubbyphp\Serialization\Registry\ObjectMappingRegistryInterface;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\NullLogger;
12
13
final class Serializer implements SerializerInterface
14
{
15
    /**
16
     * @var ObjectMappingRegistryInterface
17
     */
18
    private $objectMappingRegistry;
19
20
    /**
21
     * @var LoggerInterface
22
     */
23
    private $logger;
24
25
    /**
26
     * @param ObjectMappingRegistryInterface $objectMappingRegistry
27
     * @param LoggerInterface                $logger
28
     */
29 1
    public function __construct(ObjectMappingRegistryInterface $objectMappingRegistry, LoggerInterface $logger = null)
30
    {
31 1
        $this->objectMappingRegistry = $objectMappingRegistry;
32 1
        $this->logger = $logger ?? new NullLogger();
33 1
    }
34
35
    /**
36
     * @param Request $request
37
     * @param object  $object
38
     * @param string  $path
39
     *
40
     * @return array
41
     *
42
     * @throws NotObjectException
43
     */
44 1
    public function serialize(Request $request, $object, string $path = ''): array
45
    {
46 1
        if (!is_object($object)) {
47
            $this->logger->error('serialize: object without an object given {type}', ['type' => gettype($object)]);
48
49
            throw NotObjectException::createByType(gettype($object));
50
        }
51
52 1
        $objectClass = get_class($object);
53
54 1
        $objectMapping = $this->objectMappingRegistry->getObjectMappingForClass($objectClass);
55
56 1
        $fields = $this->serializeFields($request, $objectMapping, $object, $path);
57 1
        $embeddedFields = $this->serializeEmbeddedFields($request, $objectMapping, $object, $path);
58 1
        $links = $this->serializeLinks($request, $objectMapping, $object, $fields, $path);
59
60 1
        $data = $fields;
61 1
        if ([] !== $embeddedFields) {
62 1
            $data['_embedded'] = $embeddedFields;
63
        }
64 1
        if ([] !== $links) {
65 1
            $data['_links'] = $links;
66
        }
67
68 1
        return $data;
69
    }
70
71
    /**
72
     * @param Request                $request
73
     * @param ObjectMappingInterface $objectMapping
74
     * @param $object
75
     * @param string $path
76
     *
77
     * @return array
78
     */
79 1 View Code Duplication
    private function serializeFields(
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...
80
        Request $request,
81
        ObjectMappingInterface $objectMapping,
82
        $object,
83
        string $path
84
    ): array {
85 1
        $data = [];
86 1
        foreach ($objectMapping->getFieldMappings() as $fieldMapping) {
87 1
            $name = $fieldMapping->getName();
88 1
            $subPath = '' !== $path ? $path.'.'.$name : $name;
89
90 1
            $this->logger->info('deserialize: path {path}', ['path' => $subPath]);
91
92 1
            $data[$fieldMapping->getName()] = $fieldMapping
93 1
                ->getFieldSerializer()
94 1
                ->serializeField($subPath, $request, $object, $this);
95
        }
96
97 1
        return $data;
98
    }
99
100
    /**
101
     * @param Request                $request
102
     * @param ObjectMappingInterface $objectMapping
103
     * @param $object
104
     * @param string $path
105
     *
106
     * @return array
107
     */
108 1 View Code Duplication
    private function serializeEmbeddedFields(
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...
109
        Request $request,
110
        ObjectMappingInterface $objectMapping,
111
        $object,
112
        string $path
113
    ): array {
114 1
        $data = [];
115 1
        foreach ($objectMapping->getEmbeddedFieldMappings() as $fieldEmbeddedMapping) {
116 1
            $name = $fieldEmbeddedMapping->getName();
117 1
            $subPath = '' !== $path ? $path.'.'.$name : $name;
118
119 1
            $this->logger->info('deserialize: path {path}', ['path' => $subPath]);
120
121 1
            $data[$fieldEmbeddedMapping->getName()] = $fieldEmbeddedMapping
122 1
                ->getFieldSerializer()
123 1
                ->serializeField($subPath, $request, $object, $this);
124
        }
125
126 1
        return $data;
127
    }
128
129
    /**
130
     * @param Request                $request
131
     * @param ObjectMappingInterface $objectMapping
132
     * @param $object
133
     * @param array  $fields
134
     * @param string $path
135
     *
136
     * @return array
137
     */
138 1 View Code Duplication
    private function serializeLinks(
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...
139
        Request $request,
140
        ObjectMappingInterface $objectMapping,
141
        $object,
142
        array $fields,
143
        string $path
144
    ): array {
145 1
        $data = [];
146 1
        foreach ($objectMapping->getLinkMappings() as $linkMapping) {
147 1
            $name = $linkMapping->getName();
148 1
            $subPath = '' !== $path ? $path.'._links.'.$name : '_links.'.$name;
149
150 1
            $this->logger->info('deserialize: path {path}', ['path' => $subPath]);
151
152 1
            $data[$linkMapping->getName()] = $linkMapping
153 1
                ->getLinkSerializer()
154 1
                ->serializeLink($subPath, $request, $object, $fields)
155 1
                ->jsonSerialize();
156
        }
157
158 1
        return $data;
159
    }
160
}
161