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

TransformPystringToYamlstring::transformArgument()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 16
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 1
nop 3
crap 12
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 Behat\Gherkin\Node\PyStringNode;
16
use Symfony\Component\Yaml\Yaml;
17
use Ynlo\GraphQLBundle\Behat\Gherkin\YamlStringNode;
18
19
/**
20
 * Transform a pyString node into a YAMLStringNode
21
 * Must use YAMLStringNode has typehint in the method
22
 * and use the step as normal pystring
23
 *
24
 * @example setArray(YamlStringNode $variables){
25
 *              $variables->toArray();
26
 *              //...
27
 *         }
28
 *
29
 *          And set array:
30
 *          """
31
 *          - var1
32
 *          - var2
33
 *          - var3
34
 *          """
35
 */
36
class TransformPystringToYamlstring implements ArgumentTransformer
37
{
38
    /**
39
     * @var ArgumentTransformer[]
40
     */
41
    protected $transformers;
42
43
    public function __construct($transformers)
44
    {
45
        $this->transformers = $transformers;
46
    }
47
48
    public function supportsDefinitionAndArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
49
    {
50
        $refMethod = $definitionCall->getCallee()->getReflection();
51
        $refParam = $refMethod->getParameters()[$argumentIndex] ?? null;
52
53
        if ($argumentValue instanceof PyStringNode && $refParam && $refClass = $refParam->getClass()) {
54
            return is_a($refClass->getName(), YamlStringNode::class, true);
55
        }
56
    }
57
58
    public function transformArgument(DefinitionCall $definitionCall, $argumentIndex, $argumentValue)
59
    {
60
        /** @var PyStringNode $argumentValue */
61
        $parsedValue = Yaml::parse($argumentValue);
62
63
        //parse
64
        array_walk_recursive(
65
            $parsedValue,
66
            function (&$value) use ($definitionCall, $argumentIndex) {
67
                foreach ($this->transformers as $transformer) {
68
                    if ($transformer->supportsDefinitionAndArgument($definitionCall, $argumentIndex, $value)) {
69
                        $value = $transformer->transformArgument($definitionCall, $argumentIndex, $value);
70
                    }
71
                }
72
            }
73
        );
74
        $node = new YamlStringNode($argumentValue->getStrings(), $argumentValue->getLine());
75
        $node->setArray($parsedValue);
76
77
        return $node;
78
    }
79
}
80