Completed
Push — master ( f81cf6...7fbbb0 )
by Oleg
03:40
created

SplitArrayCodeFeeder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 36
rs 10
c 0
b 0
f 0
ccs 20
cts 20
cp 1
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B feed() 0 24 7
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Generator\Config\Code;
5
6
use Nette\PhpGenerator\ClassType;
7
use Nette\PhpGenerator\PhpLiteral;
8
use Nette\PhpGenerator\PhpNamespace;
9
use SlayerBirden\DFCodeGeneration\Code\Printer\NsArrayPrinter;
10
11
final class SplitArrayCodeFeeder implements CodeFeederInterface
12
{
13
    /**
14
     * @var ArrayPartInterface[]
15
     */
16
    private $parts;
17
18 2
    public function __construct(ArrayPartInterface ...$parts)
19
    {
20 2
        $this->parts = $parts;
21 2
    }
22
23 2
    public function feed(array $data, ClassType $class, PhpNamespace $namespace): void
24
    {
25 2
        $invoke = [];
26
27 2
        foreach ($data as $key => $config) {
28 2
            foreach ($this->parts as $part) {
29 2
                if ($part->matches($key)) {
30 2
                    $invoke[$key] = $part->getCalleeCode($key, $config, $namespace);
31 2
                    break 1;
32
                }
33
            }
34
        }
35
36 2
        $invokeBody = 'return ' . (new NsArrayPrinter($namespace))->printArray($invoke, 1, '') . ";\n";
37 2
        $class->addMethod('__invoke')
38 2
            ->setVisibility(ClassType::VISIBILITY_PUBLIC)
39 2
            ->setReturnType('array')
40 2
            ->setBody($invokeBody);
41
42 2
        foreach ($data as $key => $config) {
43 2
            foreach ($this->parts as $part) {
44 2
                if ($part->matches($key)) {
45 2
                    $part->feed($key, $config, $class, $namespace);
46 2
                    break 1;
47
                }
48
            }
49
        }
50 2
    }
51
}
52