Completed
Pull Request — 3.2 (#289)
by
unknown
10:16
created

DrupalExtension::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 105
Code Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 105
rs 8.2857
cc 1
eloc 102
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Drupal\DrupalExtension\ServiceContainer;
4
5
use Behat\Behat\Context\ServiceContainer\ContextExtension;
6
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
7
use Behat\Testwork\ServiceContainer\ExtensionManager;
8
use Behat\Testwork\ServiceContainer\ServiceProcessor;
9
use Drupal\DrupalExtension\Compiler\DriverPass;
10
use Drupal\DrupalExtension\Compiler\EventSubscriberPass;
11
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\DependencyInjection\Loader\FileLoader;
16
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
17
18
class DrupalExtension implements ExtensionInterface {
19
20
  /**
21
   * Extension configuration ID.
22
   */
23
  const DRUPAL_ID = 'drupal';
24
25
  /**
26
   * Selectors handler ID.
27
   */
28
  const SELECTORS_HANDLER_ID = 'drupal.selectors_handler';
29
30
  /**
31
   * @var ServiceProcessor
32
   */
33
  private $processor;
34
35
  /**
36
   * Initializes compiler pass.
37
   *
38
   * @param null|ServiceProcessor $processor
39
   */
40
  public function __construct(ServiceProcessor $processor = null) {
41
    $this->processor = $processor ? : new ServiceProcessor();
42
  }
43
44
  /**
45
   * {@inheritDoc}
46
   */
47
  public function getConfigKey() {
48
    return self::DRUPAL_ID;
49
  }
50
51
  /**
52
   * {@inheritDoc}
53
   */
54
  public function initialize(ExtensionManager $extensionManager) {
55
  }
56
57
  /**
58
   * {@inheritDoc}
59
   */
60
  public function load(ContainerBuilder $container, array $config) {
61
    $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/config'));
62
    $loader->load('services.yml');
63
    $container->setParameter('drupal.drupal.default_driver', $config['default_driver']);
64
65
    $this->loadParameters($container, $config);
66
67
    // Setup any drivers if requested.
68
    $this->loadBlackbox($loader, $config);
69
    $this->loadDrupal($loader, $container, $config);
70
    $this->loadDrush($loader, $container, $config);
71
  }
72
73
  /**
74
   * {@inheritDoc}
75
   */
76
  public function process(ContainerBuilder $container) {
77
    $this->processDriverPass($container);
78
    $this->processEventSubscriberPass($container);
79
    $this->processEnvironmentReaderPass($container);
80
    $this->processClassGenerator($container);
81
  }
82
83
  /**
84
   * {@inheritDoc}
85
   */
86
  public function configure(ArrayNodeDefinition $builder) {
87
    $builder->
88
      children()->
89
        scalarNode('default_driver')->
90
          defaultValue('blackbox')->
91
          info('Use "blackbox" to test remote site. See "api_driver" for easier integration.')->
92
        end()->
93
        scalarNode('api_driver')->
94
          defaultValue('drush')->
95
          info('Bootstraps drupal through "drupal8" or "drush".')->
96
        end()->
97
        scalarNode('drush_driver')->
98
          defaultValue('drush')->
99
        end()->
100
        arrayNode('region_map')->
101
          info("Targeting content in specific regions can be accomplished once those regions have been defined." . PHP_EOL
102
            . '  My region: "#css-selector"' . PHP_EOL
103
            . '  Content: "#main .region-content"'. PHP_EOL
104
            . '  Right sidebar: "#sidebar-second"'. PHP_EOL
105
          )->
106
          useAttributeAsKey('key')->
107
          prototype('variable')->
108
          end()->
109
        end()->
110
        arrayNode('text')->
111
          info(
112
              'Text strings, such as Log out or the Username field can be altered via behat.yml if they vary from the default values.' . PHP_EOL
113
            . '  log_out: "Sign out"' . PHP_EOL
114
            . '  log_in: "Sign in"' . PHP_EOL
115
            . '  password_field: "Enter your password"' . PHP_EOL
116
            . '  username_field: "Nickname"'
117
          )->
118
          addDefaultsIfNotSet()->
119
          children()->
120
            scalarNode('log_in')->
121
              defaultValue('Log in')->
122
            end()->
123
            scalarNode('log_out')->
124
              defaultValue('Log out')->
125
            end()->
126
            scalarNode('password_field')->
127
              defaultValue('Password')->
128
            end()->
129
            scalarNode('username_field')->
130
              defaultValue('Username')->
131
            end()->
132
          end()->
133
        end()->
134
        arrayNode('selectors')->
135
          addDefaultsIfNotSet()->
136
          children()->
137
            scalarNode('message_selector')->end()->
138
            scalarNode('error_message_selector')->end()->
139
            scalarNode('success_message_selector')->end()->
140
            scalarNode('warning_message_selector')->end()->
141
            scalarNode('login_form_selector')->
142
              defaultValue('form#user-login')->
143
            end()->
144
            scalarNode('logged_in_selector')->
145
              defaultValue('body.logged-in,body.user-logged-in')->
146
            end()->
147
          end()->
148
        end()->
149
        // Drupal drivers.
150
        arrayNode('blackbox')->
151
        end()->
152
        arrayNode('drupal')->
153
          children()->
154
            scalarNode('drupal_root')->end()->
155
          end()->
156
        end()->
157
        arrayNode('drush')->
158
          children()->
159
            scalarNode('alias')->end()->
160
            scalarNode('binary')->defaultValue('drush')->end()->
161
            scalarNode('root')->end()->
162
            scalarNode('global_options')->end()->
163
          end()->
164
        end()->
165
        // Subcontext paths.
166
        arrayNode('subcontexts')->
167
          info(
168
              'The Drupal Extension is capable of discovering additional step-definitions provided by subcontexts.' . PHP_EOL
169
            . 'Module authors can provide these in files following the naming convention of foo.behat.inc. Once that module is enabled, the Drupal Extension will load these.' . PHP_EOL
170
            . PHP_EOL
171
            . 'Additional subcontexts can be loaded by either placing them in the bootstrap directory (typically features/bootstrap) or by adding them to behat.yml.'
172
          )->
173
          addDefaultsIfNotSet()->
174
          children()->
175
            arrayNode('paths')->
176
              info(
177
                '- /path/to/additional/subcontexts' . PHP_EOL
178
              . '- /another/path'
179
              )->
180
              useAttributeAsKey('key')->
181
              prototype('variable')->end()->
182
            end()->
183
            scalarNode('autoload')->
184
              defaultValue(TRUE)->
185
            end()->
186
          end()->
187
        end()->
188
      end()->
189
    end();
190
  }
191
192
  /**
193
   * Load test parameters.
194
   */
195
  private function loadParameters(ContainerBuilder $container, array $config) {
196
    // Store config in parameters array to be passed into the DrupalContext.
197
    $drupal_parameters = array();
198
    foreach ($config as $key => $value) {
199
      $drupal_parameters[$key] = $value;
200
    }
201
    $container->setParameter('drupal.parameters', $drupal_parameters);
202
203
    $container->setParameter('drupal.region_map', $config['region_map']);
204
  }
205
206
  /**
207
   * Load the blackbox driver.
208
   */
209
  private function loadBlackBox(FileLoader $loader, array $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
210
    // Always include the blackbox driver.
211
    $loader->load('drivers/blackbox.yml');
212
  }
213
214
  /**
215
   * Load the Drupal driver.
216
   */
217
  private function loadDrupal(FileLoader $loader, ContainerBuilder $container, array $config) {
218
    if (isset($config['drupal'])) {
219
      $loader->load('drivers/drupal.yml');
220
      $container->setParameter('drupal.driver.drupal.drupal_root', $config['drupal']['drupal_root']);
221
    }
222
  }
223
224
  /**
225
   * Load the Drush driver.
226
   */
227
  private function loadDrush(FileLoader $loader, ContainerBuilder $container, array $config) {
228
    if (isset($config['drush'])) {
229
      $loader->load('drivers/drush.yml');
230
      if (!isset($config['drush']['alias']) && !isset($config['drush']['root'])) {
231
        throw new \RuntimeException('Drush `alias` or `root` path is required for the Drush driver.');
232
      }
233
      $config['drush']['alias'] = isset($config['drush']['alias']) ? $config['drush']['alias'] : FALSE;
234
      $container->setParameter('drupal.driver.drush.alias', $config['drush']['alias']);
235
236
      $config['drush']['binary'] = isset($config['drush']['binary']) ? $config['drush']['binary'] : 'drush';
237
      $container->setParameter('drupal.driver.drush.binary', $config['drush']['binary']);
238
239
      $config['drush']['root'] = isset($config['drush']['root']) ? $config['drush']['root'] : FALSE;
240
      $container->setParameter('drupal.driver.drush.root', $config['drush']['root']);
241
242
      // Set global arguments.
243
      $this->setDrushOptions($container, $config);
244
    }
245
  }
246
247
  /**
248
   * Set global drush arguments.
249
   */
250
  private function setDrushOptions(ContainerBuilder $container, array $config) {
251
    if (isset($config['drush']['global_options'])) {
252
      $definition = $container->getDefinition('drupal.driver.drush');
253
      $definition->addMethodCall('setArguments', array($config['drush']['global_options']));
254
    }
255
  }
256
257
  /**
258
   * Process the Driver Pass.
259
   */
260
  private function processDriverPass(ContainerBuilder $container) {
261
    $driverPass = new DriverPass();
262
    $driverPass->process($container);
263
  }
264
265
  /**
266
   * Process the Event Subscriber Pass.
267
   */
268
  private function processEventSubscriberPass(ContainerBuilder $container) {
269
    $eventSubscriberPass = new EventSubscriberPass();
270
    $eventSubscriberPass->process($container);
271
  }
272
273
  /**
274
   * Process the Environment Reader pass.
275
   */
276
  private function processEnvironmentReaderPass(ContainerBuilder $container) {
277
    // Register Behat context readers.
278
    $references = $this->processor->findAndSortTaggedServices($container, ContextExtension::READER_TAG);
279
    $definition = $container->getDefinition('drupal.context.environment.reader');
280
281
    foreach ($references as $reference) {
282
      $definition->addMethodCall('registerContextReader', array($reference));
283
    }
284
  }
285
286
  /**
287
   * Switch to custom class generator.
288
   */
289
  private function processClassGenerator(ContainerBuilder $container) {
290
    $definition = new Definition('Drupal\DrupalExtension\Context\ContextClass\ClassGenerator');
291
    $container->setDefinition(ContextExtension::CLASS_GENERATOR_TAG . '.simple', $definition);
292
  }
293
}
294