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

supportsDefinitionAndArgument()   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
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
crap 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