Completed
Push — master ( 1826e3...724c6f )
by Rafael
07:29
created

GraphQLApiExtension::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 1
nc 1
nop 1
crap 6
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;
12
13
use Behat\Testwork\ServiceContainer\Extension;
14
use Behat\Testwork\ServiceContainer\ExtensionManager;
15
use Behat\Testwork\ServiceContainer\ServiceProcessor;
16
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
17
use Symfony\Component\Config\FileLocator;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
20
use Ynlo\GraphQLBundle\Behat\Authentication\FosUserResolver;
21
use Ynlo\GraphQLBundle\Behat\Authentication\JWT\LexikJWTGenerator;
22
use Ynlo\GraphQLBundle\Behat\Transformer\TransformStringToExpression;
23
24
/**
25
 * GraphQLBundle extension for Behat.
26
 */
27
class GraphQLApiExtension implements Extension
28
{
29
    const CONTEXTS_PARAMETER = 'graphql.contexts';
30
31
    private static $config = [];
32
33
    /**
34
     * @var ServiceProcessor
35
     */
36
    private $processor;
37
38
    /**
39
     * Initializes extension.
40
     *
41
     * @param null|ServiceProcessor $processor
42
     */
43
    public function __construct(ServiceProcessor $processor = null)
44
    {
45
        $this->processor = $processor ? : new ServiceProcessor();
46
    }
47
48
    public function getConfigKey()
49
    {
50
        return 'graphql';
51
    }
52
53
    public function initialize(ExtensionManager $extensionManager)
54
    {
55
        if (!$extensionManager->getExtension('symfony2')) {
56
            throw new \RuntimeException(
57
                'The behat "Symfony2Extension" is required to work with "GraphQLApiExtension". 
58
Ensure you have "Behat\Symfony2Extension" inside your behat config file.'
59
            );
60
        }
61
    }
62
63
    public function configure(ArrayNodeDefinition $builder)
64
    {
65
        $root = $builder->addDefaultsIfNotSet()->children();
66
67
        $client = $root->arrayNode('client')->addDefaultsIfNotSet()->children();
68
        $client->booleanNode('insulated')->defaultFalse();
69
70
        $root->scalarNode('route');
71
72
        $authentication = $root->arrayNode('authentication')->addDefaultsIfNotSet()->children();
73
        $jwt = $authentication->arrayNode('jwt')->addDefaultsIfNotSet()->canBeEnabled()->children();
74
75
        $jwt->scalarNode('generator')
76
            ->defaultValue(LexikJWTGenerator::class);
77
78
        $jwt->scalarNode('user_resolver')
79
            ->defaultValue(FosUserResolver::class);
80
    }
81
82
    public function process(ContainerBuilder $container)
83
    {
84
    }
85
86
    public function load(ContainerBuilder $container, array $config)
87
    {
88
        self::$config = $config;
89
90
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/Resources/config'));
91
        $loader->load('services.yml');
92
        $container->setParameter('graphql.client_config', $config['client']);
93
94
        $this->processExpressionPreprocessors($container);
95
    }
96
97
    private function processExpressionPreprocessors(ContainerBuilder $container)
98
    {
99
        $references = $this->processor->findAndSortTaggedServices($container, 'graphql.expression_preprocessor');
100
        $definition = $container->getDefinition(TransformStringToExpression::class);
101
102
        if (!class_exists('Doctrine\Common\DataFixtures\Loader')) {
103
            $container->removeDefinition('Ynlo\GraphQLBundle\Behat\Fixtures\LoadFixturesSubscriber');
104
        }
105
106
        foreach ($references as $reference) {
107
            $definition->addMethodCall('registerPreprocessor', array($reference));
108
        }
109
    }
110
111
    /**
112
     * @return mixed
113
     */
114
    public static function getConfig(): array
115
    {
116
        return self::$config;
117
    }
118
}
119