SplitArrayCodeFeeder::feed()   B
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 16
nop 3
dl 0
loc 24
rs 8.8333
c 0
b 0
f 0
ccs 17
cts 17
cp 1
crap 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 10
    public function __construct(ArrayPartInterface ...$parts)
19
    {
20 10
        $this->parts = $parts;
21 10
    }
22
23 10
    public function feed(array $data, ClassType $class, PhpNamespace $namespace): void
24
    {
25 10
        $invoke = [];
26
27 10
        foreach ($data as $key => $config) {
28 10
            foreach ($this->parts as $part) {
29 10
                if ($part->matches($key)) {
30 10
                    $invoke[$key] = $part->getCalleeCode($key, $config, $namespace);
31 10
                    break 1;
32
                }
33
            }
34
        }
35
36 10
        $invokeBody = 'return ' . (new NsArrayPrinter($namespace))->printArray($invoke, 1, '') . ";\n";
37 10
        $class->addMethod('__invoke')
38 10
            ->setVisibility(ClassType::VISIBILITY_PUBLIC)
39 10
            ->setReturnType('array')
40 10
            ->setBody($invokeBody);
41
42 10
        foreach ($data as $key => $config) {
43 10
            foreach ($this->parts as $part) {
44 10
                if ($part->matches($key)) {
45 10
                    $part->feed($key, $config, $class, $namespace);
46 10
                    break 1;
47
                }
48
            }
49
        }
50 10
    }
51
}
52