Completed
Pull Request — 3.1 (#290)
by
unknown
09:17
created

DriverPass::process()   C

Complexity

Conditions 11
Paths 17

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 20
nc 17
nop 1

How to fix   Complexity   

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\Compiler;
4
5
use Symfony\Component\DependencyInjection\Reference,
6
    Symfony\Component\DependencyInjection\ContainerBuilder,
7
    Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
9
/**
10
 * Drupal\DrupalExtension container compilation pass.
11
 */
12
class DriverPass implements CompilerPassInterface {
13
  /**
14
   * Register Drupal drivers.
15
   */
16
  public function process(ContainerBuilder $container) {
17
    if (!$container->hasDefinition('drupal.drupal')) {
18
      return;
19
    }
20
21
    $drupalDefinition = $container->getDefinition('drupal.drupal');
22
    foreach ($container->findTaggedServiceIds('drupal.driver') as $id => $attributes) {
23
      foreach ($attributes as $attribute) {
24
        if (isset($attribute['alias']) && $name = $attribute['alias']) {
25
          $drupalDefinition->addMethodCall(
26
            'registerDriver', array($name, new Reference($id))
27
          );
28
        }
29
      }
30
31
      // If this is Drupal Driver, then a core controller needs to be
32
      // instantiated as well.
33
      if ('drupal.driver.drupal' === $id) {
34
        $drupalDriverDefinition = $container->getDefinition($id);
35
        $availableCores = array();
36
        foreach ($container->findTaggedServiceIds('drupal.core') as $coreId => $coreAttributes) {
37
          foreach ($coreAttributes as $attribute) {
38
            if (isset($attribute['alias']) && $name = $attribute['alias']) {
39
              $availableCores[$name] = $container->getDefinition($coreId);
40
            }
41
          }
42
        }
43
        $drupalDriverDefinition->addMethodCall(
44
          'setCore', array($availableCores)
45
        );
46
      }
47
    }
48
49
    $drupalDefinition->addMethodCall(
50
      'setDefaultDriverName', array($container->getParameter('drupal.drupal.default_driver'))
51
    );
52
  }
53
}
54