Completed
Branch 1.x (b5de1d)
by Khairi
09:40
created

DatasourceRegistry::getDataContainer()   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
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Khusseini\PimcoreRadBrickBundle;
4
5
use InvalidArgumentException;
6
use stdClass;
7
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
8
9
class DatasourceRegistry
10
{
11
    /**
12
     * @var ExpressionLanguage
13
     */
14
    private $expressionLangauge;
15
16
    /**
17
     * @var object
18
     */
19
    private $datasources = null;
20
21
    private $executedData = [];
22
23 19
    public function __construct(
24
        ExpressionLanguage $expressionLangauge = null
25
    ) {
26 19
        if (!$expressionLangauge) {
27 19
            $expressionLangauge = new ExpressionLanguage();
28
        }
29
30 19
        $this->expressionLangauge = $expressionLangauge;
31 19
        $this->datasources = new stdClass();
32 19
    }
33
34
    /**
35
     * @param array<string> $args
36
     *
37
     * @return mixed
38
     */
39 7
    public function execute(string $name, array $args = [])
40
    {
41 7
        if (!$this->has($name)) {
42 1
            throw new InvalidArgumentException(sprintf('Datasource \'%s\' not found.', $name));
43
        }
44
45 6
        $ds = $this->datasources->{$name};
46 6
        $this->executedData[$name] = $ds(...$args);
47
48 6
        return $this->executedData[$name];
49
    }
50
51 2
    public function hasData(string $name): bool
52
    {
53 2
        return isset($this->executedData[$name]) && (bool) $this->executedData[$name];
54
    }
55
56 1
    public function getDataContainer(): array
57
    {
58 1
        return $this->executedData;
59
    }
60
61 7
    public function has(string $name): bool
62
    {
63 7
        return property_exists($this->datasources, $name);
64
    }
65
66 2
    public function getData(string $name, bool $execute = false, array $execArgs = [])
67
    {
68 2
        if ($execute) {
69 1
            $this->execute($name, $execArgs);
70
        }
71
72 2
        if (!$this->hasData($name)) {
73 1
            return [];
74
        }
75
76 2
        return $this->executedData[$name];
77
    }
78
79 6
    public function executeAll(): \Generator
80
    {
81 6
        $ds = (array) $this->datasources;
82 6
        foreach ($ds as $name => $callback) {
83 6
            $this->executedData[$name] = $callback();
84 6
            $data = $this->executedData[$name];
85 6
            yield $name => $data;
86
        }
87 6
    }
88
89 13
    public function add(string $name, callable $callable): void
90
    {
91 13
        $this->datasources->{$name} = $callable;
92 13
    }
93
94
    /**
95
     * @param array<string> $args
96
     */
97 10
    public function createMethodCall(
98
        object $service,
99
        string $method,
100
        array $args
101
    ): callable {
102
        return function (array $input) use ($service, $method, $args) {
103 9
            foreach ($args as $index => $expression) {
104 9
                $args[$index] = $this->getValueRecursive($input, $expression);
105
            }
106
107 9
            return \call_user_func_array([$service, $method], $args);
108 10
        };
109
    }
110
111 9
    protected function getValueRecursive(array $input, $expression)
112
    {
113 9
        if (\is_string($expression)) {
114 9
            return $this->getValue($input, $expression);
115
        }
116
117 4
        if (\is_array($expression)) {
118 4
            foreach ($expression as $key => $value) {
119 4
                $expression[$key] = $this->getValueRecursive($input, $value);
120
            }
121
        }
122
123 4
        return $expression;
124
    }
125
126
    /**
127
     * @param array<array> $context
128
     *
129
     * @return mixed
130
     */
131 9
    public function getValue(array $context, string $expression)
132
    {
133
        try {
134 9
            return $this->expressionLangauge->evaluate($expression, $context);
135 1
        } catch (\Exception $ex) {
136 1
            return $expression;
137
        }
138
    }
139
}
140