Call::provideEvaluator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\ExpressionLanguage\Functions;
5
6
use Closure;
7
use SmartWeb\ModuleTesting\Contracts\ExpressionLanguage\Validator;
8
use SmartWeb\ModuleTesting\ExpressionLanguage\CallbackValidator;
9
use function call_user_func_array;
10
11
12
/**
13
 * Expression function for invoking custom callbacks
14
 *
15
 * @package SmartWeb\ModuleTesting\ExpressionLanguage\Functions
16
 */
17
class Call extends BaseExpressionFunction
18
{
19
    
20
    /**
21
     * @var Validator
22
     */
23
    private $callbackValidator;
24
    
25
    /**
26
     * @inheritDoc
27
     */
28
    protected function init()
29
    {
30
        $this->callbackValidator = new CallbackValidator("ExpressionFunction '{$this->getName()}' expects parameter 1 to be a valid callback");
31
    }
32
    
33
    /**
34
     * @inheritDoc
35
     */
36
    protected function provideCompiler() : callable
37
    {
38
        return function (...$args) : string
39
        {
40
            return sprintf('call_user_func(%s)', implode(', ', $args));
41
        };
42
    }
43
    
44
    /**
45
     * @inheritDoc
46
     */
47
    protected function provideEvaluator() : callable
48
    {
49
        /**
50
         * @param array            $arguments
51
         * @param callable|Closure $callback
52
         *
53
         * @return mixed
54
         */
55
        $evaluator = function (array $arguments, $callback)
56
        {
57
            $this->callbackValidator->validate($callback);
58
            
59
            return call_user_func_array($callback, $arguments['params']);
60
        };
61
        
62
        return $evaluator;
63
    }
64
    
65
}
66