InflectorBuilder::buildInflector()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 4
rs 10
1
<?php
2
3
namespace Maiorano\ContainerConfig;
4
5
use League\Container\Inflector\InflectorAggregateInterface;
6
use League\Container\Inflector\InflectorInterface;
7
8
/**
9
 * Class InflectorBuilder.
10
 */
11
final class InflectorBuilder implements BuilderInterface
12
{
13
    /**
14
     * @var InflectorAggregateInterface
15
     */
16
    private InflectorAggregateInterface $inflectors;
17
18
    /**
19
     * InflectorBuilder constructor.
20
     *
21
     * @param InflectorAggregateInterface $inflectors
22
     */
23 6
    public function __construct(InflectorAggregateInterface $inflectors)
24
    {
25 6
        $this->inflectors = $inflectors;
26 6
    }
27
28
    /**
29
     * @param array $config
30
     *
31
     * @return InflectorAggregateInterface
32
     */
33 1
    public function build(array $config): InflectorAggregateInterface
34
    {
35 1
        foreach ($config as $key => $value) {
36 1
            $this->buildInflector((string) $key, $value);
37
        }
38
39 1
        return $this->inflectors;
40
    }
41
42
    /**
43
     * @param string                $key
44
     * @param string|array|callable $value
45
     *
46
     * @return InflectorInterface
47
     */
48 5
    public function buildInflector(string $key, $value): InflectorInterface
49
    {
50 5
        $inflector = $this->inflectors->add($key, $this->resolveCallback($value));
51
52 5
        if (is_string($value)) {
53 1
            $inflector->invokeMethod($value, []);
54
        }
55 5
        if (is_array($value) && !is_callable($value)) {
56 3
            $this->resolveProperties($inflector, $value['properties'] ?? []);
57 3
            $this->resolveMethods($inflector, $value['methods'] ?? []);
58
        }
59
60 5
        return $inflector;
61
    }
62
63
    /**
64
     * @param mixed $value
65
     *
66
     * @return callable|null
67
     */
68 5
    private function resolveCallback($value): ?callable
69
    {
70 5
        if (is_callable($value)) {
71 1
            return $value;
72 4
        } elseif (!is_array($value)) {
73 1
            return null;
74
        }
75
76 3
        return $value['callback'] ?? null;
77
    }
78
79
    /**
80
     * @param InflectorInterface $inflector
81
     * @param array              $properties
82
     *
83
     * @return InflectorInterface
84
     */
85 3
    private function resolveProperties(InflectorInterface $inflector, array $properties): InflectorInterface
86
    {
87 3
        return $inflector->setProperties($properties);
88
    }
89
90
    /**
91
     * @param InflectorInterface $inflector
92
     * @param array              $methods
93
     *
94
     * @return InflectorInterface
95
     */
96 3
    private function resolveMethods(InflectorInterface $inflector, array $methods): InflectorInterface
97
    {
98 3
        return $inflector->invokeMethods($methods);
99
    }
100
}
101