Completed
Push — master ( 714b6a...f9d007 )
by Narcotic
06:13
created

FieldMapper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 46.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 53
ccs 7
cts 15
cp 0.4667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addMapper() 0 4 1
A buildFields() 0 14 3
A map() 0 7 2
1
<?php
2
/**
3
 * map fields using multiple mappers
4
 */
5
6
namespace Graviton\GeneratorBundle\Generator\ResourceGenerator;
7
8
use Graviton\GeneratorBundle\Definition\JsonDefinition;
9
10
/**
11
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
12
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
13
 * @link     http://swisscom.ch
14
 */
15
class FieldMapper implements FieldMapperInterface
16
{
17
    /**
18
     * @var FieldMapperInterface[]
19
     */
20
    private $mappers = [];
21
22
    /**
23
     * @param FieldMapperInterface $mapper mapper to add
24
     *
25
     * @return void
26
     */
27 6
    public function addMapper(FieldMapperInterface $mapper)
28
    {
29 6
        $this->mappers[] = $mapper;
30 6
    }
31
32
    /**
33
     * builds the initial fields array with a json definition
34
     *
35
     * @param JsonDefinition $jsonDefinition definition
36
     *
37
     * @return array fields
38
     */
39
    public function buildFields(JsonDefinition $jsonDefinition)
40
    {
41
        $fields = [];
42
        foreach ($jsonDefinition->getFields() as $field) {
43
            if ($field->getName() != 'id') {
44
                $fields[] = [
45
                    'fieldName' => $field->getName(),
46
                    'type' => $field->getTypeDoctrine()
47
                ];
48
            }
49
        }
50
51
        return $fields;
52
    }
53
54
    /**
55
     * @param array $field   mappable field with type attribute
56
     * @param mixed $context context for mapper to check
57
     *
58
     * @return array
59
     */
60 2
    public function map($field, $context = null)
61
    {
62 2
        foreach ($this->mappers as $mapper) {
63 2
            $field = $mapper->map($field, $context);
64
        }
65 2
        return $field;
66
    }
67
}
68