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

FixturePreprocessor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 29
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B setUp() 0 17 5
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
}