Completed
Pull Request — master (#16)
by Sergii
03:53
created

TqExtension::load()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 5
cts 5
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 2
crap 5
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\ServiceContainer;
6
7
use Behat\EnvironmentLoader;
8
use Behat\Testwork\ServiceContainer\Extension;
9
use Behat\Testwork\ServiceContainer\ExtensionManager;
10
use Behat\DebugExtension\ServiceContainer\DebugExtension;
11
use Drupal\Driver\DrupalDriver;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
14
15
/**
16
 * Class TqExtension.
17
 *
18
 * @package Drupal\TqExtension\ServiceContainer
19
 */
20
class TqExtension implements Extension
21
{
22
    /**
23 20
     * {@inheritDoc}
24
     */
25 20
    public function getConfigKey()
26
    {
27
        return 'tq';
28
    }
29
30
    /**
31 4
     * {@inheritDoc}
32
     */
33 4
    public function initialize(ExtensionManager $extensionManager)
34 4
    {
35 4
        if (null === $extensionManager->getExtension(DebugExtension::TAG)) {
36 4
            $extensionManager->activateExtension('Behat\DebugExtension');
37
        }
38
    }
39
40
    /**
41 4
     * {@inheritDoc}
42
     */
43 4
    public function load(ContainerBuilder $container, array $config)
44 4
    {
45 4
        if (!$container->has('drupal.driver.drupal')) {
46
            throw new \LogicException(
47
                'TqExtension is completely depends on DrupalExtension and must be configured after it.'
48
            );
49
        }
50
51
        /** @var DrupalDriver $drupalDriver */
52 4
        $sourceDir = dirname(__DIR__);
53
        $drupalDriver = $container->get('drupal.driver.drupal');
54 4
        $drupalDriver->setCoreFromVersion();
55
56
        if (!defined('DRUPAL_CORE')) {
57
            define('DRUPAL_CORE', (int) $drupalDriver->version);
58
        }
59
60
        if (!class_exists('DrupalKernelPlaceholder')) {
61
            foreach (['base', DRUPAL_CORE] as $filename) {
62
                require_once "$sourceDir/Cores/$filename.php";
63
            }
64
        }
65
66
        $loader = new EnvironmentLoader($this, $container, $config);
67
        $loader->load();
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     *
73
     * @see EnvironmentExtension::getEnvironmentReaderId()
74
     */
75 8
    public function process(ContainerBuilder $container)
76
    {
77 4
    }
78
79
    /**
80
     * {@inheritDoc}
81 4
     *
82 4
     * @link http://symfony.com/doc/current/components/config/definition.html
83 4
     *
84
     * @example
85 4
     * Drupal\TqExtension:
86 4
     *   wait_for_redirect: 60
87 4
     *   email_account_strings: get_account_strings_for_email
88
     *   email_accounts:
89 4
     *     account_alias:
90 4
     *       imap: imap.gmail.com:993/imap/ssl
91 4
     *       email: [email protected]
92 4
     *       password: p4sswDstr_1
93 8
     *     administrator:
94 4
     *       imap: imap.gmail.com:993/imap/ssl
95 4
     *       email: [email protected]
96 4
     *       password: p4sswDstr_2
97 4
     */
98
    public function configure(ArrayNodeDefinition $builder)
99 4
    {
100 4
        $config = $builder->children();
101 4
102 8
        foreach ([
103
            'wait_for_redirect' => [
104
                'defaultValue' => 30,
105 4
                'info' => 'The timeout (in seconds) for waiting opening a page',
106 4
            ],
107 4
            'wait_for_email' => [
108 4
                'defaultValue' => 30,
109 4
                'info' => 'This timeout will be used if you checking an email via IMAP',
110 4
            ],
111 4
            'email_account_strings' => [
112 4
                'defaultValue' => '',
113 4
                'info' => 'See detailed description in "docs/examples/EMAIL.md"',
114 4
            ],
115
        ] as $scalarNode => $data) {
116 4
            $config = $config->scalarNode($scalarNode)
117 4
                ->defaultValue($data['defaultValue'])
118
                ->info($data['info'])
119
                ->end();
120
        }
121
122
        $config = $config->arrayNode('email_accounts')
123
            ->requiresAtLeastOneElement()
124
            ->prototype('array')
125
            ->children();
126
127
        foreach ([
128
            'imap' => 'IMAP url without parameters. For example: imap.gmail.com:993/imap/ssl',
129
            'username' => 'Login from an e-mail account',
130
            'password' => 'Password from an e-mail account',
131
        ] as $scalarNode => $info) {
132
            $config = $config->scalarNode($scalarNode)
133
                ->isRequired()
134
                ->cannotBeEmpty()
135
                ->info($info)
136
                ->end();
137
        }
138
139
        $config->end()->end()->end()->end();
140
    }
141
}
142