Passed
Pull Request — master (#1)
by Flavien
03:14
created

getCurrentClassName()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
crap 12
1
<?php
2
3
namespace QualityCode\TransformAndLoadBundle\Services\Import;
4
5
class SchemaDefinitionGroupByEntity
6
{
7
    protected $schemaDefinitionByEntity = array();
8
9
    /**
10
     * @param array $schemaDefinition
11
     *
12
     * @return array
13
     */
14
    public function convert(array $schemaDefinition)
15
    {
16
        $this->schemaDefinitionByEntity[$schemaDefinition['main_entity']] = array();
17
18
        foreach ($schemaDefinition['fields'] as $key => $values) {
19
            $className = $this->getCurrentClassName($schemaDefinition['main_entity'], $values);
20
            $this->schemaDefinitionByEntity[$className][$key] = $this->getCleanValues($values);
21
        }
22
23
        return $this->schemaDefinitionByEntity;
24
    }
25
26
    /**
27
     * @param array $values
28
     *
29
     * @return array
30
     */
31
    protected function getCleanValues(array $values)
32
    {
33
        unset($values['class']);
34
        unset($values['link_entity']);
35
36
        return $values;
37
    }
38
39
    /**
40
     * @param string $mainClassName
41
     * @param array  $value
42
     *
43
     * @return string
44
     */
45
    protected function getCurrentClassName(string $mainClassName, array $value)
46
    {
47
        if (array_key_exists('link_entity', $value) && $value['link_entity'] === true) {
48
            return $this->checkIfAClassIsPresentInTheSchemaDefinition($value['class']);
49
        }
50
51
        return $mainClassName;
52
    }
53
54
    /**
55
     * @param string $className
56
     *
57
     * @return string
58
     */
59
    protected function checkIfAClassIsPresentInTheSchemaDefinition(string $className)
60
    {
61
        if (!array_key_exists($className, $this->schemaDefinitionByEntity)) {
62
            $this->schemaDefinitionByEntity[$className] = array();
63
        }
64
65
        return $className;
66
    }
67
}
68