Completed
Push — master ( 2f038b...509b2e )
by Łukasz
13s queued 12s
created

FriendsOfBehatSymfonyExtensionExtension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FriendsOfBehat\SymfonyExtension\Bundle\DependencyInjection;
6
7
use Behat\Behat\Context\Context;
8
use Behat\Mink\Mink;
9
use Behat\Mink\Session;
10
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
11
use Symfony\Component\BrowserKit\Client;
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Symfony\Component\DependencyInjection\Definition;
16
use Symfony\Component\DependencyInjection\Reference;
17
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18
19
final class FriendsOfBehatSymfonyExtensionExtension extends Extension implements CompilerPassInterface
20
{
21
    public function load(array $configs, ContainerBuilder $container): void
22
    {
23
        $this->provideMinkIntegration($container);
24
        $this->registerBehatContainer($container);
25
26
        $container->registerForAutoconfiguration(Context::class)->addTag('fob.context');
27
    }
28
29
    public function process(ContainerBuilder $container): void
30
    {
31
        $this->provideBrowserKitIntegration($container);
32
33
        foreach ($container->findTaggedServiceIds('fob.context') as $serviceId => $attributes) {
34
            $serviceDefinition = $container->findDefinition($serviceId);
35
36
            $serviceDefinition->setPublic(true);
37
            $serviceDefinition->clearTag('fob.context');
38
        }
39
    }
40
41
    private function registerBehatContainer(ContainerBuilder $container): void
42
    {
43
        $behatServiceContainerDefinition = new Definition(ContainerInterface::class);
44
        $behatServiceContainerDefinition->setPublic(true);
45
        $behatServiceContainerDefinition->setSynthetic(true);
46
47
        $container->setDefinition('behat.service_container', $behatServiceContainerDefinition);
48
    }
49
50
    private function provideBrowserKitIntegration(ContainerBuilder $container): void
51
    {
52
        if (!class_exists(Client::class) || !$container->has('test.client')) {
53
            return;
54
        }
55
56
        $container->setAlias(Client::class, 'test.client');
57
    }
58
59
    private function provideMinkIntegration(ContainerBuilder $container): void
60
    {
61
        if (!class_exists(Mink::class)) {
62
            return;
63
        }
64
65
        $this->registerMink($container);
66
        $this->registerMinkDefaultSession($container);
67
        $this->registerMinkParameters($container);
68
    }
69
70 View Code Duplication
    private function registerMink(ContainerBuilder $container): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $minkDefinition = new Definition(Mink::class, ['fob_symfony.mink']);
73
        $minkDefinition->setPublic(true);
74
        $minkDefinition->setLazy(true);
75
        $minkDefinition->setFactory([new Reference('behat.service_container'), 'get']);
76
77
        $container->setDefinition('behat.mink', $minkDefinition);
78
        $container->setAlias(Mink::class, 'behat.mink');
79
    }
80
81 View Code Duplication
    private function registerMinkDefaultSession(ContainerBuilder $container): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $minkDefaultSessionDefinition = new Definition(Session::class);
84
        $minkDefaultSessionDefinition->setPublic(true);
85
        $minkDefaultSessionDefinition->setLazy(true);
86
        $minkDefaultSessionDefinition->setFactory([new Reference('behat.mink'), 'getSession']);
87
88
        $container->setDefinition('behat.mink.default_session', $minkDefaultSessionDefinition);
89
        $container->setAlias(Session::class, 'behat.mink.default_session');
90
    }
91
92 View Code Duplication
    private function registerMinkParameters(ContainerBuilder $container): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $minkParametersDefinition = new Definition(MinkParameters::class, ['fob_symfony.mink.parameters']);
95
        $minkParametersDefinition->setPublic(true);
96
        $minkParametersDefinition->setLazy(true);
97
        $minkParametersDefinition->setFactory([new Reference('behat.service_container'), 'get']);
98
99
        $container->setDefinition('behat.mink.parameters', $minkParametersDefinition);
100
        $container->setAlias(MinkParameters::class, 'behat.mink.parameters');
101
    }
102
}
103