Completed
Pull Request — master (#157)
by
unknown
12:36
created

DriverEntityDrupal8::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 5
1
<?php
2
3
namespace Drupal\Driver\Wrapper\Entity;
4
5
use Drupal\Driver\Plugin\DriverPluginManagerInterface;
6
use Drupal\Driver\Plugin\DriverNameMatcher;
7
8
/**
9
 * A Driver wrapper for Drupal 8 entities.
10
 */
11
class DriverEntityDrupal8 extends DriverEntityBase implements DriverEntityWrapperInterface {
12
13
  /**
14
   * The Drupal version being driven.
15
   *
16
   * @var int
17
   */
18
  protected $version = 8;
19
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function __construct(
24
        $type,
25
        $bundle = NULL,
26
        DriverPluginManagerInterface $entityPluginManager = NULL,
27
        DriverPluginManagerInterface $fieldPluginManager = NULL,
28
        $projectPluginRoot = NULL
29
    ) {
30
    // Set Drupal environment variables used by default plugin manager.
31
    $this->namespaces = \Drupal::service('container.namespaces');
0 ignored issues
show
Bug introduced by
The property namespaces does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
    $this->cache_backend = $cache_backend = \Drupal::service('cache.discovery');
0 ignored issues
show
Bug introduced by
The property cache_backend does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
$cache_backend is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
    $this->module_handler = $module_handler = \Drupal::service('module_handler');
0 ignored issues
show
Bug introduced by
The property module_handler does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
$module_handler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34
35
    parent::__construct($type, $bundle, $entityPluginManager, $fieldPluginManager, $projectPluginRoot);
36
  }
37
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public static function create(array $fields, $type, $bundle = NULL) {
42
    $entity = new DriverEntityDrupal8(
43
        $type,
44
        $bundle
45
    );
46
    $entity->setFields($fields);
47
    return $entity;
48
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function setBundle($identifier) {
54
    // Don't set a bundle if the entity doesn't support bundles.
55
    $supportsBundles = $this->getProvisionalPlugin()->supportsBundles();
56
    if ($supportsBundles) {
57
      $bundles = $this->getProvisionalPlugin()->getBundles();
58
      $matcher = new DriverNameMatcher($bundles);
59
      $result = $matcher->identify($identifier);
60
      if (is_null($result)) {
61
        throw new \Exception("'$identifier' could not be identified as a bundle of the '" . $this->getEntityTypeId() . "' entity type.");
62
      }
63
      parent::setBundle($result);
64
    }
65
    return $this;
66
  }
67
68
  /**
69
   * Set the entity type.
70
   *
71
   * @param string $identifier
72
   *   A human-friendly name for an entity type .
73
   */
74
  protected function setType($identifier) {
75
    $typeDefinitions = \Drupal::EntityTypeManager()->getDefinitions();
76
    $candidates = [];
77
    foreach ($typeDefinitions as $machineName => $typeDefinition) {
78
      $label = (string) $typeDefinition->getLabel();
79
      $candidates[$label] = $machineName;
80
    }
81
    $matcher = new DriverNameMatcher($candidates);
82
    $result = $matcher->identify($identifier);
83
    if (is_null($result)) {
84
      throw new \Exception("'$identifier' could not be identified as an entity type.");
85
    }
86
    $this->type = $result;
87
  }
88
89
}
90