Passed
Push — master ( e37ab6...297052 )
by Koldo
02:10
created

FactoryTranslator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 86.11%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 56
ccs 31
cts 36
cp 0.8611
rs 10
c 0
b 0
f 0
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 54 10
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 1
    public function process(array &$symfonyFactory): array
16
    {
17 1
        $factories = [];
18
19 1
        foreach ($symfonyFactory['services'] ?? [] as $serviceName => $service) {
20 1
            if (!is_array($service)) {
21
                continue;
22
            }
23 1
            if (array_key_exists('factory', $service) && array_key_exists('arguments', $service)) {
24 1
                if (is_string($service['factory'])) {
25 1
                    $factories[$serviceName] = [
26 1
                        $service['factory'],
27 1
                        '__invoke',
28 1
                        (new ParseArguments())->process(
29 1
                            $symfonyFactory,
30 1
                            $service
31
                        ),
32
                    ];
33 1
                    unset($symfonyFactory['services'][$serviceName]);
34 1
                    continue;
35
                }
36 1
                if (is_array($service['factory'])) {
37 1
                    $factory = array_shift($service['factory']);
38 1
                    $method = array_shift($service['factory']);
39 1
                    $factories[$serviceName] = [
40 1
                        $factory,
41 1
                        $method,
42 1
                        (new ParseArguments())->process(
43 1
                            $symfonyFactory,
44 1
                            $service
45
                        ),
46
                    ];
47 1
                    unset($symfonyFactory['services'][$serviceName]);
48 1
                    continue;
49
                }
50
            }
51 1
            if (array_key_exists('factory', $service)) {
52 1
                if (is_string($service['factory'])) {
53 1
                    $factories[$serviceName] = $service['factory'];
54 1
                    unset($symfonyFactory['services'][$serviceName]);
55 1
                    continue;
56
                }
57
58
                if (is_array($service['factory'])) {
59
                    $factories[$serviceName] = $service['factory'];
60
                    unset($symfonyFactory['services'][$serviceName]);
61
                    continue;
62
                }
63
            }
64
        }
65
66
        return [
67
            'dependencies' => [
68 1
                'factories' => $factories,
69
            ],
70
        ];
71
    }
72
}
73