Passed
Pull Request — master (#1)
by Flavien
06:34 queued 03:23
created

SchemaDefinitionGroupByEntity   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 63
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 11 2
A getCleanValues() 0 7 1
A getCurrentClassName() 0 8 3
A checkIfAClassIsPresentInTheSchemaDefinition() 0 8 2
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