FieldFormatter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 7
c 3
b 1
f 2
lcom 1
cbo 0
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A formatRelationMappings() 0 12 3
A getTypes() 0 10 1
A formatMappingsWithRelations() 0 6 1
A formatMappings() 0 10 2
1
<?php
2
3
namespace Kpicaza\GenBundle\Formatter;
4
5
use Doctrine\ORM\Mapping\ClassMetadataInfo;
6
7
/**
8
 * Class FieldFormatter.
9
 */
10
class FieldFormatter
11
{
12
    /**
13
     * @param $mappings
14
     * @param $associationMappings
15
     *
16
     * @return mixed
17
     */
18
    public function formatMappingsWithRelations($mappings, $associationMappings)
19
    {
20
        $formattedMappings = $this->formatMappings($mappings);
21
22
        return $this->formatRelationMappings($formattedMappings, $associationMappings);
23
    }
24
25
    /**
26
     * @param $mappings
27
     *
28
     * @return mixed
29
     */
30
    public function formatMappings($mappings)
31
    {
32
        $types = $this->getTypes();
33
34
        foreach ($mappings as $key => $mapping) {
35
            $mappings[$key] = $types[$mappings[$key]['type']];
36
        }
37
38
        return $mappings;
39
    }
40
41
    /**
42
     * @param $mappings
43
     * @param $associationMappings
44
     *
45
     * @return mixed
46
     */
47
    protected function formatRelationMappings($mappings, $associationMappings)
48
    {
49
        $types = $this->getTypes();
50
51
        foreach ($associationMappings as $fieldName => $relation) {
52
            if ($relation['type'] !== ClassMetadataInfo::ONE_TO_MANY) {
53
                $mappings[sprintf('%s_%s', $fieldName, 'id')] = $types['string'];
54
            }
55
        }
56
57
        return $mappings;
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    protected function getTypes()
64
    {
65
        return array(
66
            'string' => 'Type\\TextType::class',
67
            'text' => 'Type\\TextareaType::class',
68
            'datetime' => 'Type\\DateTimeType::class',
69
            'integer' => 'Type\\NumberType::class',
70
            'boolean' => 'Type\\NumberType::class',
71
        );
72
    }
73
}
74