MappingTransformer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 34
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A transform() 0 9 3
A applyTransformer() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ImportBundle\Transformer;
6
7
use Darkilliant\ImportBundle\Registry\TransformerRegistry;
8
9
class MappingTransformer
10
{
11
    /** @var TransformerRegistry */
12
    private $registry;
13
14 1
    public function __construct(TransformerRegistry $registry)
15
    {
16 1
        $this->registry = $registry;
17 1
    }
18
19 1
    public function transform(array $data, array $mapping)
20
    {
21 1
        foreach ($mapping as $key => $config) {
22 1
            $data[$key] = (is_array($config))
23 1
                ? $this->applyTransformer((string) $key, $config['value'], $config['transformers'])
24 1
                : $config;
25
        }
26
27 1
        return $data;
28
    }
29
30 1
    private function applyTransformer(string $name, $value, $transformers)
31
    {
32 1
        foreach ($transformers as $transformerKey => $transformerConfig) {
33 1
            $transformerName = (is_array($transformerConfig)) ? $transformerKey : $transformerConfig;
34 1
            $transformerOptions = (is_array($transformerConfig)) ? $transformerConfig : [];
35
36 1
            $transformer = $this->registry->get($transformerName);
37 1
            $transformer->validate($value, $name, $transformerOptions);
38
39 1
            $value = $transformer->transform($value, $name, $transformerOptions);
40
        }
41
42 1
        return $value;
43
    }
44
}
45