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

TqExtension::load()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 5
cts 5
cp 1
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 13
nc 5
nop 2
crap 4
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
        foreach (['base', DRUPAL_CORE] as $filename) {
61
            require_once "$sourceDir/Cores/$filename.php";
62
        }
63
64
        $loader = new EnvironmentLoader($this, $container, $config);
65
        $loader->load();
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     *
71
     * @see EnvironmentExtension::getEnvironmentReaderId()
72
     */
73
    public function process(ContainerBuilder $container)
74
    {
75 8
    }
76
77 4
    /**
78
     * {@inheritDoc}
79
     *
80
     * @link http://symfony.com/doc/current/components/config/definition.html
81 4
     *
82 4
     * @example
83 4
     * Drupal\TqExtension:
84
     *   wait_for_redirect: 60
85 4
     *   email_account_strings: get_account_strings_for_email
86 4
     *   email_accounts:
87 4
     *     account_alias:
88
     *       imap: imap.gmail.com:993/imap/ssl
89 4
     *       email: [email protected]
90 4
     *       password: p4sswDstr_1
91 4
     *     administrator:
92 4
     *       imap: imap.gmail.com:993/imap/ssl
93 8
     *       email: [email protected]
94 4
     *       password: p4sswDstr_2
95 4
     */
96 4
    public function configure(ArrayNodeDefinition $builder)
97 4
    {
98
        $config = $builder->children();
99 4
100 4
        foreach ([
101 4
            'wait_for_redirect' => [
102 8
                'defaultValue' => 30,
103
                'info' => 'The timeout (in seconds) for waiting opening a page',
104
            ],
105 4
            'wait_for_email' => [
106 4
                'defaultValue' => 30,
107 4
                'info' => 'This timeout will be used if you checking an email via IMAP',
108 4
            ],
109 4
            'email_account_strings' => [
110 4
                'defaultValue' => '',
111 4
                'info' => 'See detailed description in "docs/examples/EMAIL.md"',
112 4
            ],
113 4
        ] as $scalarNode => $data) {
114 4
            $config = $config->scalarNode($scalarNode)
115
                ->defaultValue($data['defaultValue'])
116 4
                ->info($data['info'])
117 4
                ->end();
118
        }
119
120
        $config = $config->arrayNode('email_accounts')
121
            ->requiresAtLeastOneElement()
122
            ->prototype('array')
123
            ->children();
124
125
        foreach ([
126
            'imap' => 'IMAP url without parameters. For example: imap.gmail.com:993/imap/ssl',
127
            'username' => 'Login from an e-mail account',
128
            'password' => 'Password from an e-mail account',
129
        ] as $scalarNode => $info) {
130
            $config = $config->scalarNode($scalarNode)
131
                ->isRequired()
132
                ->cannotBeEmpty()
133
                ->info($info)
134
                ->end();
135
        }
136
137
        $config->end()->end()->end()->end();
138
    }
139
}
140