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

SplitArrayCodeFeeder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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