EntityNormalizer   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 8
dl 0
loc 165
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 33 1
A normalizeFields() 0 9 3
B normalizeEmbeddables() 0 12 5
B normalizeAssociations() 0 12 5
A isFieldSerializable() 0 10 3
A isEmbeddableSerializable() 0 10 3
A isAssociationSerializable() 0 10 3
A supportsNormalization() 0 8 2
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 * 
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 * 
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Component\Serializer;
14
15
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16
use WellCommerce\Component\Serializer\Metadata\Collection\AssociationMetadataCollection;
17
use WellCommerce\Component\Serializer\Metadata\Collection\FieldMetadataCollection;
18
19
/**
20
 * Class EntityNormalizer
21
 *
22
 * @author  Adam Piotrowski <[email protected]>
23
 */
24
class EntityNormalizer extends AbstractSerializer implements NormalizerInterface
25
{
26
    public function normalize($object, $format = null, array $context = [])
27
    {
28
        $this->format                     = $format;
29
        $this->context                    = $context;
30
        $data                             = [];
31
        $entityMetadata                   = $this->getEntityMetadata($object);
32
        $serializationMetadata            = $this->getSerializationMetadata($object);
33
        $serializedFieldsCollection       = $serializationMetadata->getFields();
34
        $serializedAssociationsCollection = $serializationMetadata->getAssociations();
35
        
36
        $this->normalizeFields(
37
            $this->getEntityFields($entityMetadata),
38
            $serializedFieldsCollection,
39
            $object,
40
            $data
41
        );
42
        
43
        $this->normalizeEmbeddables(
44
            $this->getEntityEmbeddables($entityMetadata),
45
            $serializedFieldsCollection,
46
            $object,
47
            $data
48
        );
49
        
50
        $this->normalizeAssociations(
51
            $this->getEntityAssociations($entityMetadata),
52
            $serializedAssociationsCollection,
53
            $object,
54
            $data
55
        );
56
        
57
        return $data;
58
    }
59
    
60
    /**
61
     * Normalizes the entity's fields
62
     *
63
     * @param array                   $fields
64
     * @param FieldMetadataCollection $collection
65
     * @param object                  $object
66
     * @param array                   $data
67
     */
68
    protected function normalizeFields(array $fields, FieldMetadataCollection $collection, $object, array &$data)
69
    {
70
        foreach ($fields as $field) {
71
            if ($this->isFieldSerializable($field, $collection)) {
72
                $value = $this->propertyAccessor->getValue($object, $field);
73
                $this->propertyAccessor->setValue($data, $this->getPropertyPath($field), $value);
74
            }
75
        }
76
    }
77
    
78
    /**
79
     * Normalizes the entity's embeddables
80
     *
81
     * @param array                   $embeddables
82
     * @param FieldMetadataCollection $collection
83
     * @param object                  $object
84
     * @param array                   $data
85
     */
86
    protected function normalizeEmbeddables(array $embeddables, FieldMetadataCollection $collection, $object, array &$data)
87
    {
88
        foreach ($embeddables as $embedabbleName) {
89
            if ($this->isEmbeddableSerializable($embedabbleName, $collection)) {
90
                $value = $this->propertyAccessor->getValue($object, $embedabbleName);
91
                if (null !== $value && !is_scalar($value)) {
92
                    $value = $this->serializer->normalize($value, $this->format, $this->context);
93
                }
94
                $this->propertyAccessor->setValue($data, $this->getPropertyPath($embedabbleName), $value);
95
            }
96
        }
97
    }
98
    
99
    /**
100
     * Normalizes the entity's associations
101
     *
102
     * @param array                         $associations
103
     * @param AssociationMetadataCollection $collection
104
     * @param object                        $object
105
     * @param array                         $data
106
     */
107
    protected function normalizeAssociations(array $associations, AssociationMetadataCollection $collection, $object, array &$data)
108
    {
109
        foreach ($associations as $association) {
110
            if ($this->isAssociationSerializable($association, $collection)) {
111
                $value = $this->propertyAccessor->getValue($object, $association);
112
                if (null !== $value && !is_scalar($value)) {
113
                    $value = $this->serializer->normalize($value, $this->format, $this->context);
114
                }
115
                $this->propertyAccessor->setValue($data, $this->getPropertyPath($association), $value);
116
            }
117
        }
118
    }
119
    
120
    /**
121
     * Checks whether the field is serializable
122
     *
123
     * @param string                  $fieldName
124
     * @param FieldMetadataCollection $collection
125
     *
126
     * @return bool
127
     */
128
    protected function isFieldSerializable(string $fieldName, FieldMetadataCollection $collection): bool
129
    {
130
        if ($collection->has($fieldName)) {
131
            $field = $collection->get($fieldName);
132
            
133
            return ($field->hasGroup($this->context['group']) || $field->hasGroup('default'));
134
        }
135
        
136
        return false;
137
    }
138
    
139
    /**
140
     * Checks whether the embeddable field is serializable
141
     *
142
     * @param string                  $embeddableName
143
     * @param FieldMetadataCollection $collection
144
     *
145
     * @return bool
146
     */
147
    protected function isEmbeddableSerializable(string $embeddableName, FieldMetadataCollection $collection): bool
148
    {
149
        if ($collection->has($embeddableName)) {
150
            $embeddable = $collection->get($embeddableName);
151
            
152
            return ($embeddable->hasGroup($this->context['group']) || $embeddable->hasGroup('default'));
153
        }
154
        
155
        return false;
156
    }
157
    
158
    /**
159
     * Checks whether the association is serializable
160
     *
161
     * @param string                        $associationName
162
     * @param AssociationMetadataCollection $collection
163
     *
164
     * @return bool
165
     */
166
    protected function isAssociationSerializable(string $associationName, AssociationMetadataCollection $collection): bool
167
    {
168
        if ($collection->has($associationName)) {
169
            $association = $collection->get($associationName);
170
            
171
            return ($association->hasGroup($this->context['group']) || $association->hasGroup('default'));
172
        }
173
        
174
        return false;
175
    }
176
    
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function supportsNormalization($data, $format = null)
181
    {
182
        if (!is_object($data)) {
183
            return false;
184
        }
185
        
186
        return $this->hasSerializationMetadata($data);
187
    }
188
}
189