Passed
Push — master ( 40a4c6...6a0114 )
by Koldo
02:21
created

FactoryTranslator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 30
dl 0
loc 51
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFactoryWithArguments() 0 23 3
A process() 0 22 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\SymfonyConfigTranslator;
6
7
use function array_key_exists;
8
use function array_shift;
9
use function dump;
10
use function is_array;
11
use function is_string;
12
13
class FactoryTranslator
14
{
15 3
    public function process(array &$symfonyFactory): array
16
    {
17 3
        $factories = [];
18
19 3
        foreach ($symfonyFactory['services'] ?? [] as $serviceName => $service) {
20 3
            if (!is_array($service)) {
21 1
                continue;
22
            }
23 2
            if (array_key_exists('factory', $service) && array_key_exists('arguments', $service)) {
24 1
                $factories[$serviceName] = $this->getFactoryWithArguments($symfonyFactory, $serviceName, $service);
25 1
                continue;
26
            }
27 2
            if (array_key_exists('factory', $service)) {
28 2
                $factories[$serviceName] = $service['factory'];
29 2
                unset($symfonyFactory['services'][$serviceName]);
30 2
                continue;
31
            }
32
        }
33
34
        return [
35
            'dependencies' => [
36 3
                'factories' => $factories,
37
            ],
38
        ];
39
    }
40
41 1
    private function getFactoryWithArguments(array &$symfonyFactory, string $serviceName, $service): array
42
    {
43 1
        $config = [];
44 1
        if (is_string($service['factory'])) {
45 1
            $arguments = (new ArgumentTranslator())->process(
46 1
                $symfonyFactory,
47 1
                $service
48
            );
49 1
            unset($symfonyFactory['services'][$serviceName]);
50 1
            $config = [$service['factory'], '__invoke', $arguments];
51
        }
52 1
        if (is_array($service['factory'])) {
53 1
            $factory = array_shift($service['factory']);
54 1
            $method = array_shift($service['factory']);
55 1
            $arguments = (new ArgumentTranslator())->process(
56 1
                $symfonyFactory,
57 1
                $service
58
            );
59 1
            unset($symfonyFactory['services'][$serviceName]);
60 1
            $config = [$factory, $method, $arguments];
61
        }
62
63 1
        return $config;
64
    }
65
}
66