Passed
Pull Request — master (#993)
by Maxim
20:44 queued 10:42
created

StemplerConfig::wire()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0416
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Stempler\Config;
6
7
use Spiral\Core\Container\Autowire;
8
use Spiral\Core\InjectableConfig;
9
use Spiral\Stempler\Exception\ConfigException;
10
11
final class StemplerConfig extends InjectableConfig
12
{
13
    public const CONFIG = 'views/stempler';
14
15
    protected array $config = [
16
        'directives' => [],
17
        'processors' => [],
18
        'visitors'   => [],
19
    ];
20
21
    /**
22
     * @return Autowire[]
23
     */
24 42
    public function getDirectives(): array
25
    {
26 42
        $directives = [];
27 42
        foreach ($this->config['directives'] as $directive) {
28 42
            if (\is_object($directive) && !$directive instanceof Autowire) {
29
                $directives[] = $directive;
30
                continue;
31
            }
32
33 42
            $directives[] = $this->wire($directive);
34
        }
35
36 42
        return $directives;
37
    }
38
39
    /**
40
     * @return Autowire[]
41
     */
42 42
    public function getProcessors(): array
43
    {
44 42
        $processors = [];
45 42
        foreach ($this->config['processors'] as $processor) {
46 42
            if (is_object($processor) && !$processor instanceof Autowire) {
47
                $processors[] = $processor;
48
                continue;
49
            }
50
51 42
            $processors[] = $this->wire($processor);
52
        }
53
54 42
        return $processors;
55
    }
56
57 43
    public function getVisitors(int $stage): array
58
    {
59 43
        $visitors = [];
60 43
        foreach ($this->config['visitors'][$stage] ?? [] as $visitor) {
61 43
            if (\is_object($visitor) && !$visitor instanceof Autowire) {
62
                $visitors[] = $visitor;
63
                continue;
64
            }
65
66 43
            $visitors[] = $this->wire($visitor);
67
        }
68
69 43
        return $visitors;
70
    }
71
72
    /**
73
     * @throws ConfigException
74
     */
75 49
    private function wire(mixed $item): Autowire
76
    {
77 49
        if ($item instanceof Autowire) {
78 1
            return $item;
79
        }
80
81 48
        if (\is_string($item)) {
82 48
            return new Autowire($item);
83
        }
84
85
        throw new ConfigException('Invalid class reference in view config');
86
    }
87
}
88