Manager::map()   C
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.004

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 31
ccs 22
cts 23
cp 0.9565
rs 6.7272
cc 7
eloc 19
nc 4
nop 3
crap 7.004
1
<?php
2
3
namespace GFG\Mapper\Data;
4
5
class Manager
6
{
7
    /**
8
     * @var MapperInterface
9
     */
10
    private $mapper;
11
12
    /**
13
     * @var MapperFactory
14
     */
15
    private $factory;
16
17
    /**
18
     * @param MapperInterface $mapper
19
     * @param MapperFactory $factory
20
     */
21 1
    public function __construct(
22
        MapperInterface $mapper,
23
        MapperFactory $factory
24
    ) {
25 1
        $this->mapper    = $mapper;
26 1
        $this->factory   = $factory;
27 1
    }
28
29
    /**
30
     * Map a given data based on a given structure
31
     *
32
     * @param array $data Data to be mapped
33
     * @param array $structure How to map
34
     * @param string $basePrefix
35
     * @return void will change $data by reference
36
     */
37 1
    public function map(array &$data, array $structure, $basePrefix = null)
38
    {
39 1
        foreach ($structure as $key => $value) {
40
41 1
            if (!isset($data[$key])) {
42
                continue;
43
            }
44
45 1
            $keyData = &$data[$key];
46
47 1
            if (!isset($value['prefix'])) {
48 1
                $this->map($keyData, $value);
49 1
            } else {
50 1
                foreach ((array) $value['type'] as $type) {
51 1
                    $use   = isset($value['use'])   ? $value['use']   : '';
52 1
                    $inner = isset($value['inner']) ? $value['inner'] : '';
53 1
                    $type  = $this->factory->create(
54 1
                        $this,
55 1
                        $type,
56 1
                        $this->mapper,
57
                        [
58 1
                            'prefix' => $basePrefix . $value['prefix'],
59 1
                            'use'    => $use,
60
                            'inner'  => $inner
61 1
                        ]
62 1
                    );
63 1
                    $type->run($keyData);
64 1
                }
65
            }
66 1
        }
67 1
    }
68
}
69