Completed
Pull Request — master (#396)
by
unknown
01:25
created

DrupalSubContextBase::getContext()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\DrupalExtension\Context\DrupalSubContextBase.
6
 */
7
8
namespace Drupal\DrupalExtension\Context;
9
10
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
11
use Drupal\DrupalDriverManager;
12
13
/**
14
 * Base class for subcontexts that use the Drupal API.
15
 */
16
abstract class DrupalSubContextBase extends RawDrupalContext implements DrupalSubContextInterface {
17
18
  /**
19
   * The Drupal Driver Manager.
20
   *
21
   * @var \Drupal\DrupalDriverManager $drupal
22
   */
23
  protected $drupal;
24
25
  /**
26
   * Constructs a DrupalSubContextBase object.
27
   *
28
   * @param \Drupal\DrupalDriverManager $drupal
29
   *   The Drupal driver manager.
30
   */
31
  public function __construct(DrupalDriverManager $drupal) {
32
    $this->drupal = $drupal;
33
  }
34
35
  /**
36
   * Get the currently logged in user from DrupalContext.
37
   *
38
   * @deprecated
39
   *   Deprecated in 4.x, will be removed before 5.x.
40
   *   The currently logged in user is now available in all context classes.
41
   *   Use $this->getUserManager()->getCurrentUser() instead.
42
   */
43
  protected function getUser() {
44
    trigger_error('DrupalSubContextBase::getUser() is deprecated. Use RawDrupalContext::getUserManager()->getCurrentUser() instead.', E_USER_DEPRECATED);
45
46
    $user = $this->getUserManager()->getCurrentUser();
47
48
    if (empty($user)) {
49
      throw new \Exception('No user is logged in.');
50
    }
51
52
    return $user;
53
  }
54
55
}
56