Completed
Push — master ( a1f222...7ceccc )
by Emmanuel
03:50
created

Expression::evaluateExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Edyan\Neuralyzer\Utils;
4
5
use Psr\Container\ContainerInterface;
6
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
7
8
class Expression
9
{
10
    /**
11
     * Container injected by autowiring
12
     * @var ContainerInterface
13
     */
14
    private $container;
15
16
    /**
17
     * List of "things" to inject in expressions evaluation
18
     * @var array
19
     */
20
    private $services = [];
21
22
    /**
23
     * Used for autowiring
24
     * @param  ContainerInterface $container
25
     */
26
    public function __construct(ContainerInterface $container)
27
    {
28
        $this->container = $container;
29
        $this->configure();
30
    }
31
32
    /**
33
     * Return all registered services
34
     * @return array
35
     */
36
    public function getServices(): array
37
    {
38
        return $this->services;
39
    }
40
41
    /**
42
     * Evaluate an expression that would be in the most general case
43
     * an action coming from an Anonymization config
44
     *
45
     * @param  string  $expression
46
     */
47
    public function evaluateExpression(string $expression)
48
    {
49
        $expressionLanguage = new ExpressionLanguage();
50
51
        return $expressionLanguage->evaluate($expression, $this->services);
52
    }
53
54
55
    /**
56
    * Evaluate a list of expression
57
    * @param  array  $expressions
58
    */
59
    public function evaluateExpressions(array $expressions)
60
    {
61
        $res = [];
62
        foreach ($expressions as $expression) {
63
            $res[] = $this->evaluateExpression($expression);
64
        }
65
66
        return $res;
67
    }
68
69
    /**
70
     * Configure that service by registering all services in an array
71
     */
72
    private function configure()
73
    {
74
        $services = array_keys($this->container->findTaggedServiceIds('app.service'));
0 ignored issues
show
Bug introduced by
The method findTaggedServiceIds() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as Symfony\Component\Depend...aggedContainerInterface or Symfony\Component\Depend...ection\ContainerBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        $services = array_keys($this->container->/** @scrutinizer ignore-call */ findTaggedServiceIds('app.service'));
Loading history...
75
        foreach ($services as $service) {
76
            $service = $this->container->get($service);
77
            $this->services[$service->getName()] = $service;
78
        }
79
    }
80
}
81