Passed
Pull Request — master (#9)
by Rafael
03:29
created

TransformStringToExpression   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 32
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsDefinitionAndArgument() 0 3 1
A transformArgument() 0 13 2
A registerPreprocessor() 0 3 1
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Behat\Transformer;
12
13
use Behat\Behat\Definition\Call\DefinitionCall;
14
use Behat\Behat\Transformation\Transformer\ArgumentTransformer;
15
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16
use Ynlo\GraphQLBundle\Behat\Transformer\ExpressionLanguage\ExpressionPreprocessorInterface;
17
18
/**
19
 * Allow the use of expressions (Symfony Expression Language) inside any argument
20
 * The expression must be placed inside {...}
21
 *
22
 * Use preprocessors to improve expressions.
23
 *
24
 * @see Ynlo\GraphQLBundle\Behat\Transformer\ExpressionLanguage\*
25
 */
26
class TransformStringToExpression implements ArgumentTransformer
27
{
28
    private const PATTERN = '/^{.*}$/';
29
30
    /**
31
     * @var ExpressionPreprocessorInterface[]
32
     */
33
    protected $preprocessors = [];
34
35 1
    public function supportsDefinitionAndArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
36
    {
37 1
        return (bool) preg_match(self::PATTERN, $argumentValue);
38
    }
39
40 1
    public function transformArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
41
    {
42 1
        $values = [];
43 1
        $expression = $argumentValue;
44 1
        $el = new ExpressionLanguage();
45
46 1
        foreach ($this->preprocessors as $preprocessor) {
47 1
            $preprocessor->setUp($el, $expression, $values);
48
        }
49
50 1
        $expression = preg_replace('/^{(.*)}$/', '$1', $expression);
51
52 1
        return $el->evaluate($expression, $values);
53
    }
54
55 1
    public function registerPreprocessor(ExpressionPreprocessorInterface $preprocessor)
56
    {
57 1
        $this->preprocessors[] = $preprocessor;
58 1
    }
59
}
60