Passed
Push — master ( bb1ea3...0da7d5 )
by Mike
02:05
created

TranslatorFieldHandlerPlugin   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 65
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handleCallableConfig() 0 5 1
A __construct() 0 3 1
A handleSimpleConfig() 0 9 3
A handleNestedConfig() 0 9 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Xervice\Processor\Communication\Plugin;
5
6
7
use Xervice\ArrayHandler\Dependency\FieldHandlerPluginInterface;
8
9
class TranslatorFieldHandlerPlugin implements FieldHandlerPluginInterface
10
{
11
    /**
12
     * @var \Xervice\Processor\Business\Model\Translator\TranslatorInterface[]
13
     */
14
    private $translators;
15
16
    /**
17
     * TranslatorFieldHandlerPlugin constructor.
18
     *
19
     * @param \Xervice\Processor\Business\Model\Translator\TranslatorInterface[] $translators
20
     */
21 3
    public function __construct(array $translators)
22
    {
23 3
        $this->translators = $translators;
24 3
    }
25
26
    /**
27
     * @param array $data
28
     * @param string $fieldName
29
     * @param string $config
30
     *
31
     * @return array
32
     */
33 1
    public function handleSimpleConfig(array $data, string $fieldName, string $config): array
34
    {
35 1
        foreach ($this->translators as $translator) {
36 1
            if ($translator->getName() === $fieldName) {
37 1
                $data = $translator->translate($data);
38
            }
39
        }
40
41 1
        return $data;
42
    }
43
44
    /**
45
     * @param array $data
46
     * @param string $fieldName
47
     * @param array $config
48
     *
49
     * @return array
50
     */
51 1
    public function handleNestedConfig(array $data, string $fieldName, array $config): array
52
    {
53 1
        foreach ($this->translators as $translator) {
54 1
            if ($translator->getName() === $fieldName) {
55 1
                $data = $translator->translate($data, $config);
56
            }
57
        }
58
59 1
        return $data;
60
    }
61
62
    /**
63
     * @param array $data
64
     * @param string $fieldName
65
     * @param callable $config
66
     *
67
     * @return array
68
     */
69 1
    public function handleCallableConfig(array $data, string $fieldName, callable $config): array
70
    {
71 1
        $data[$fieldName] = $config($data);
72
73 1
        return $data;
74
    }
75
}