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

FixturePreprocessor::setUp()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 6
nop 3
crap 30
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\ExpressionLanguage;
12
13
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
14
use Ynlo\GraphQLBundle\Behat\Fixtures\FixtureManager;
15
16
/**
17
 * Allow the use of fixtures inside expressions
18
 *
19
 * #fixtureName = to get the global id of existent fixture
20
 * @fixtureName = to get reference to given fixture name
21
 *
22
 * @example     "{@user1}" => User (object)
23
 * @example     "{[#user1, #user2]}" => ["VXNlcjox", "VXNlcjoy"]
24
 * @example     "{@user.getUsername()}" => "admin"
25
 */
26
class FixturePreprocessor implements ExpressionPreprocessorInterface
27
{
28
    /**
29
     * @var FixtureManager
30
     */
31
    private $fixtureManager;
32
33
    public function __construct(FixtureManager $fixtureManager)
34
    {
35
        $this->fixtureManager = $fixtureManager;
36
    }
37
38
    public function setUp(ExpressionLanguage $el, string &$expression, array &$values)
39
    {
40
        // parse fixtures IDs
41
        preg_match_all('/#(\w+)/', $expression, $matches);
42
        if ($matches[0] ?? false) {
43
            foreach ($matches[0] as $index => $match) {
44
                $expression = preg_replace('/#(\w+)/', '$1_id', $expression);
45
                $values[$matches[1][$index].'_id'] = $this->fixtureManager->getFixtureGlobalId($matches[1][$index]);
46
            }
47
        }
48
49
        // parse fixtures references
50
        preg_match_all('/@(\w+)/', $expression, $matches);
51
        if ($matches[0] ?? false) {
52
            foreach ($matches[0] as $index => $match) {
53
                $expression = preg_replace('/@(\w+)/', '$1', $expression);
54
                $values[$matches[1][$index]] = $this->fixtureManager->getFixture($matches[1][$index]);
55
            }
56
        }
57
    }
58
}