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

DrupalDriverManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 132
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A registerDriver() 0 4 1
B getDriver() 0 20 5
A setDefaultDriverName() 0 9 2
A getDrivers() 0 3 1
A setEnvironment() 0 3 1
A getEnvironment() 0 3 1
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\DrupalDriverManager.
6
 */
7
8
namespace Drupal;
9
10
use Behat\Testwork\Environment\Environment;
11
use Drupal\Driver\DriverInterface;
12
13
/**
14
 * Drupal driver manager.
15
 */
16
class DrupalDriverManager {
17
18
  /**
19
   * The name of the default driver.
20
   *
21
   * @var string
22
   */
23
  private $defaultDriverName;
24
25
  /**
26
   * All registered drivers.
27
   *
28
   * @var \Drupal\Driver\DriverInterface[]
29
   */
30
  private $drivers = array();
31
32
  /**
33
   * Behat environment.
34
   *
35
   * @var \Behat\Testwork\Environment\Environment
36
   */
37
  private $environment;
38
39
  /**
40
   * Initialize the driver manager.
41
   *
42
   * @param \Drupal\Driver\DriverInterface[] $drivers
43
   *   An array of drivers to register.
44
   */
45
  public function __construct(array $drivers = array()) {
46
    foreach ($drivers as $name => $driver) {
47
      $this->registerDriver($name, $driver);
48
    }
49
  }
50
51
  /**
52
   * Register a new driver.
53
   *
54
   * @param string $name
55
   *   Driver name.
56
   * @param \Drupal\Driver\DriverInterface $driver
57
   *   An instance of a DriverInterface.
58
   */
59
  public function registerDriver($name, DriverInterface $driver) {
60
    $name = strtolower($name);
61
    $this->drivers[$name] = $driver;
62
  }
63
64
  /**
65
   * Return a registered driver by name, or the default driver.
66
   *
67
   * @param string $name
68
   *   The name of the driver to return. If omitted the default driver is
69
   *   returned.
70
   *
71
   * @return \Drupal\Driver\DriverInterface
72
   *   The requested driver.
73
   *
74
   * @throws \InvalidArgumentException
75
   *   Thrown when the requested driver is not registered.
76
   */
77
  public function getDriver($name = NULL) {
78
    $name = strtolower($name) ?: $this->defaultDriverName;
79
80
    if (NULL === $name) {
81
      throw new \InvalidArgumentException('Specify a Drupal driver to get.');
82
    }
83
84
    if (!isset($this->drivers[$name])) {
85
      throw new \InvalidArgumentException(sprintf('Driver "%s" is not registered', $name));
86
    }
87
88
    $driver = $this->drivers[$name];
89
90
    // Bootstrap driver if needed.
91
    if (!$driver->isBootstrapped()) {
92
      $driver->bootstrap();
93
    }
94
95
    return $driver;
96
  }
97
98
  /**
99
   * Set the default driver name.
100
   *
101
   * @param string $name
102
   *   Default driver name to set.
103
   *
104
   * @throws \InvalidArgumentException
105
   *   Thrown when the driver is not registered.
106
   */
107
  public function setDefaultDriverName($name) {
108
    $name = strtolower($name);
109
110
    if (!isset($this->drivers[$name])) {
111
      throw new \InvalidArgumentException(sprintf('Driver "%s" is not registered.', $name));
112
    }
113
114
    $this->defaultDriverName = $name;
115
  }
116
117
  /**
118
   * Returns all registered drivers.
119
   *
120
   * @return \Drupal\Driver\DriverInterface[]
121
   *   An array of drivers.
122
   */
123
  public function getDrivers() {
124
    return $this->drivers;
125
  }
126
127
  /**
128
   * Sets the Behat Environment.
129
   *
130
   * @param \Behat\Testwork\Environment\Environment $environment
131
   *   The Behat Environment to set.
132
   */
133
  public function setEnvironment(Environment $environment) {
134
    $this->environment = $environment;
135
  }
136
137
  /**
138
   * Returns the Behat Environment.
139
   *
140
   * @return \Behat\Testwork\Environment\Environment
141
   *   The Behat Environment.
142
   */
143
  public function getEnvironment() {
144
    return $this->environment;
145
  }
146
147
}
148