Completed
Push — master ( 48cf76...5a3c62 )
by Dominik
06:34 queued 01:19
created

Serializer::serializeEmbeddedFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 15

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 20
loc 20
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 4
crap 3
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
62 1
        if ([] !== $embeddedFields) {
63 1
            $data['_embedded'] = $embeddedFields;
64
        }
65 1
        if ([] !== $links) {
66 1
            $data['_links'] = $links;
67
        }
68
69 1
        return [$objectMapping->getName() => $data];
70
    }
71
72
    /**
73
     * @param Request                $request
74
     * @param ObjectMappingInterface $objectMapping
75
     * @param $object
76
     * @param string $path
77
     *
78
     * @return array
79
     */
80 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...
81
        Request $request,
82
        ObjectMappingInterface $objectMapping,
83
        $object,
84
        string $path
85
    ): array {
86 1
        $data = [];
87 1
        foreach ($objectMapping->getFieldMappings() as $fieldMapping) {
88 1
            $name = $fieldMapping->getName();
89 1
            $subPath = '' !== $path ? $path.'.'.$name : $name;
90
91 1
            $this->logger->info('deserialize: path {path}', ['path' => $subPath]);
92
93 1
            $data[$fieldMapping->getName()] = $fieldMapping
94 1
                ->getFieldSerializer()
95 1
                ->serializeField($subPath, $request, $object, $this);
96
        }
97
98 1
        return $data;
99
    }
100
101
    /**
102
     * @param Request                $request
103
     * @param ObjectMappingInterface $objectMapping
104
     * @param $object
105
     * @param string $path
106
     *
107
     * @return array
108
     */
109 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...
110
        Request $request,
111
        ObjectMappingInterface $objectMapping,
112
        $object,
113
        string $path
114
    ): array {
115 1
        $data = [];
116 1
        foreach ($objectMapping->getEmbeddedFieldMappings() as $fieldEmbeddedMapping) {
117 1
            $name = $fieldEmbeddedMapping->getName();
118 1
            $subPath = '' !== $path ? $path.'.'.$name : $name;
119
120 1
            $this->logger->info('deserialize: path {path}', ['path' => $subPath]);
121
122 1
            $data[$fieldEmbeddedMapping->getName()] = $fieldEmbeddedMapping
123 1
                ->getFieldSerializer()
124 1
                ->serializeField($subPath, $request, $object, $this);
125
        }
126
127 1
        return $data;
128
    }
129
130
    /**
131
     * @param Request                $request
132
     * @param ObjectMappingInterface $objectMapping
133
     * @param $object
134
     * @param array  $fields
135
     * @param string $path
136
     *
137
     * @return array
138
     */
139 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...
140
        Request $request,
141
        ObjectMappingInterface $objectMapping,
142
        $object,
143
        array $fields,
144
        string $path
145
    ): array {
146 1
        $data = [];
147 1
        foreach ($objectMapping->getLinkMappings() as $linkMapping) {
148 1
            $name = $linkMapping->getName();
149 1
            $subPath = '' !== $path ? $path.'._links.'.$name : '_links.'.$name;
150
151 1
            $this->logger->info('deserialize: path {path}', ['path' => $subPath]);
152
153 1
            $data[$linkMapping->getName()] = $linkMapping
154 1
                ->getLinkSerializer()
155 1
                ->serializeLink($subPath, $request, $object, $fields)
156 1
                ->jsonSerialize();
157
        }
158
159 1
        return $data;
160
    }
161
}
162