Language   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82.76%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
c 4
b 0
f 0
lcom 1
cbo 3
dl 0
loc 153
ccs 24
cts 29
cp 0.8276
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 109 4
A evaluate() 0 8 2
A getFunctionNames() 0 4 1
A getParameterNames() 0 8 1
1
<?php
2
3
namespace BrainExe\Expression;
4
5
use BrainExe\Annotations\Annotations\Service;
6
use BrainExe\Core\EventDispatcher\AbstractEvent;
7
use BrainExe\Core\EventDispatcher\Events\TimingEvent;
8
use BrainExe\Core\Traits\EventDispatcherTrait;
9
use BrainExe\Core\Traits\LoggerTrait;
10
use BrainExe\InputControl\InputControlEvent;
11
use Exception;
12
use ReflectionClass;
13
use Symfony\Component\ExpressionLanguage\Expression;
14
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
15
use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface;
16
17
/**
18
 * @Service("Expression.Language", public=false, lazy=true)
19
 */
20
class Language extends ExpressionLanguage
21
{
22
23
    use EventDispatcherTrait;
24
    use LoggerTrait;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 4
    public function __construct(ParserCacheInterface $cache = null, array $providers = [])
30
    {
31 4
        parent::__construct($cache, $providers);
32
33
        $functions = [
34 4
            'sprintf',
35
            'date',
36
            'time',
37
            'microtime',
38
            'rand'
39
        ];
40
41 4
        foreach ($functions as $function) {
42
            $this->register($function, function () use ($function) {
43 1
                $parameters = func_get_args();
44
45 1
                return sprintf('%s(%s)', $function, implode(', ', $parameters));
46
            }, function () use ($function) {
47 1
                $parameters = array_slice(func_get_args(), 1);
48 1
                return call_user_func_array($function, $parameters);
49 4
            });
50
        }
51
52
        $this->register('setProperty', function ($property, $value) {
53
            return sprintf('($entity->payload[%s] = %s)', $property, $value);
54
        }, function ($parameters, $property, $value) {
55
            /** @var Entity $entity */
56
            $entity = $parameters['entity'];
57
            $entity->payload[$property] = $value;
58 4
        });
59
60
        $this->register('getProperty', function ($property) {
61
            return sprintf('$entity->payload[%s]', $property);
62
        }, function ($parameters, $property) {
63
            /** @var Entity $entity */
64
            $entity = $parameters['entity'];
65
66
            return $entity->payload[$property];
67 4
        });
68
69
        $this->register('isTiming', function ($timingId) {
70
            return sprintf('($eventName == \'%s\' && $event->timingId === %s)', TimingEvent::TIMING_EVENT, $timingId);
71
        }, function ($parameters, $eventId) {
72
            if ($parameters['eventName'] !== TimingEvent::TIMING_EVENT) {
73
                return false;
74
            }
75
76
            return $parameters['event']->timingId === $eventId;
77 4
        });
78
79
        $this->register('isEvent', function ($eventId) {
80
            return sprintf('($eventName == \'%s\')', $eventId);
81
        }, function ($parameters, $eventId) {
82
            return $parameters['eventName'] === $eventId;
83 4
        });
84
85
        $this->register('pushViaWebsocket', function () {
86
            throw new Exception('pushViaWebsocket() not implemented');
87
        }, function ($parameters) {
88
            $this->getDispatcher()->dispatchAsWebsocketEvent($parameters['event']);
89 4
        });
90
91
        $this->register('exec', function () {
92
            throw new Exception('exec() not implemented');
93
        }, function ($parameters, $string) {
94
            unset($parameters);
95
            $inputEvent = new InputControlEvent($string);
96
97
            $this->getDispatcher()->dispatchInBackground($inputEvent);
98 4
        });
99
100
        $this->register('event', function () {
101
            throw new Exception('event() not implemented');
102
        }, function (array $parameters, $type, ...$eventArguments) {
103
            unset($parameters);
104
            $events = (include ROOT.'/cache/events.php'); // TODO extract
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
105
106
            $reflection = new ReflectionClass($events[$type]['class']);
107
            /** @var AbstractEvent $event */
108
            $event = $reflection->newInstanceArgs($eventArguments);
109
110
            $this->dispatchEvent($event);
111 4
        });
112
113
        $this->register('log', function () {
114
            throw new Exception('log() not implemented');
115
        }, function (array $parameters, $level, $message, $context = null) {
116
            unset($parameters);
117
            $this->log($level, $message, ['channel' => $context]);
118 4
        });
119
120
        $this->register('increaseCounter', function () {
121
            throw new Exception('log() not implemented');
122
        }, function (array $parameters) {
123
            /** @var Entity $entity */
124
            $entity = $parameters['entity'];
125
            if (empty($entity->payload['counter'])) {
126
                $entity->payload['counter'] = 1;
127
            } else {
128
                $entity->payload['counter']++;
129
            }
130 4
        });
131
132
        $this->register('executeExpression', function () {
133
            throw new Exception('executeExpression() not implemented');
134 4
        }, function (array $parameters, $expressionId) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expressionId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
            // TODO throw some event
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
136 4
        });
137 4
    }
138
139
    /**
140
     * @param string|Expression $expression
141
     * @param array $values
142
     * @return string
143
     */
144 1
    public function evaluate($expression, $values = array())
145
    {
146 1
        if (!$expression) {
147
            return '';
148
        }
149
150 1
        return parent::evaluate($expression, $values);
151
    }
152
153
    /**
154
     * @return string[]
155
     */
156
    public function getFunctionNames()
157
    {
158
        return array_keys($this->functions);
159
    }
160
161
    /**
162
     * @return string[]
163
     */
164
    public function getParameterNames()
165
    {
166
        return [
167
            'event',
168
            'eventName',
169
            'entity'
170
        ];
171
    }
172
}
173