Completed
Push — standalone ( 88d8ec...763a1b )
by Philip
04:53 queued 40s
created

Normalizer::normalize()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 66
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 12.0071

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 66
ccs 26
cts 27
cp 0.963
rs 5.9123
cc 12
eloc 34
nc 10
nop 4
crap 12.0071

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Service;
4
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\Common\Util\ClassUtils;
7
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata;
8
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata;
9
use Metadata\MetadataFactoryInterface;
10
11
class Normalizer
12
{
13
    /**
14
     * @var MetadataFactoryInterface
15
     */
16
    private $metadataFactory;
17
18 24
    function __construct(MetadataFactoryInterface $metadataFactory)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20 24
        $this->metadataFactory = $metadataFactory;
21 24
    }
22
23
    /**
24
     * @param mixed    $data
25
     * @param string[] $includes
26
     * @param int      $depth
27
     * @param string   $path
28
     *
29
     * @return array
30
     */
31 24
    public function normalize($data, $includes = [], int $depth = 0, string $path = '')
32
    {
33 24
        if (is_array($data)) {
34 14
            $normalizedData = [];
35 14
            foreach ($data as $datum) {
36 14
                $normalizedData[] = $this->normalize($datum, $includes, $depth + 1, $path);
37
            }
38
39 14
            return $normalizedData;
40
        }
41
42 24
        if (is_object($data)) {
43
44 24
            $normalizedData = [];
45
46
            /** @var ClassMetadata $classMetadata */
47 24
            $classMetadata = $this->metadataFactory->getMetadataForClass(ClassUtils::getClass($data));
48
49
            /** @var PropertyMetadata $propertyMetadatum */
50 24
            foreach ($classMetadata->propertyMetadata as $propertyMetadatum) {
51
52 24
                if ($propertyMetadatum->isExcluded()) {
53 12
                    continue;
54
                }
55
56 24
                if ($propertyMetadatum->isAssociation()) {
57
58
                    /* Inlude if includable AND it is on include path */
59 18
                    if ($propertyMetadatum->isIncludable() && $this->isIncluded(
60
                            $path,
61 18
                            $propertyMetadatum->getIncludablePaths(),
62
                            $includes
63
                        )
64
                    ) {
65 4
                        $value = $propertyMetadatum->getValue($data);
66 4
                        if ($propertyMetadatum->isCollection()) {
67
                            /** @var Collection $value */
68 2
                            $value = $value->getValues();
69
                        }
70 4
                        $normalizedData[$propertyMetadatum->name] = $this->normalize(
71
                            $value,
72
                            $includes,
73 4
                            $depth + 1,
74 18
                            $this->appendPath($path, $propertyMetadatum->name)
75
                        );
76
                    }
77
                } else {
78
79
                    /* Inlude if includable is missing OR it is on include path */
80 24
                    if (!$propertyMetadatum->isIncludable() || $this->isIncluded(
81
                            $path,
82 24
                            $propertyMetadatum->getIncludablePaths(),
83
                            $includes
84
                        )
85
                    ) {
86 24
                        $value = $propertyMetadatum->getValue($data);
87 24
                        $normalizedData[$propertyMetadatum->name] = $this->normalizeField($value, $propertyMetadatum);
88
                    }
89
                }
90
            }
91
92 24
            return $normalizedData;
93
        }
94
95
        return null;
96
    }
97
98 18
    private function isIncluded($currentPath, array $paths, ?array $includes): bool
99
    {
100 18
        if (null === $includes) {
101
            return false;
102
        }
103
104 18
        foreach ($paths as $path) {
105 18
            if (in_array($this->appendPath($currentPath, $path), $includes)) {
106 18
                return true;
107
            }
108
        }
109
110 18
        return false;
111
    }
112
113 18
    private function appendPath($path, $name)
114
    {
115 18
        if (null === $path || '' === $path) {
116 18
            return $name;
117
        }
118
119 4
        return $path . '.' . $name;
120
    }
121
122 24
    private function normalizeField($value, PropertyMetadata $propertyMetadata)
123
    {
124 24
        switch ($propertyMetadata->getType()) {
125 24
            case 'datetime':
126 10
                if (null === $value) {
127 4
                    return null;
128
                }
129
130
                /** @var $value \DateTime */
131 8
                return $value->format('Y-m-d H:i:s');
132
133 24
            case 'date':
134 10
                if (null === $value) {
135 4
                    return null;
136
                }
137
138
                /** @var $value \DateTime */
139 8
                return $value->format('Y-m-d');
140
141 24
            case 'time':
142 10
                if (null === $value) {
143 4
                    return null;
144
                }
145
146
                /** @var $value \DateTime */
147 8
                return $value->format('H:i:s');
148
149
            default:
150 24
                return $value;
151
        }
152
    }
153
}
154