Completed
Push — 8.1.x ( fb062c...e20c53 )
by Antonio
06:58
created

Drupal8::getEntityIdByLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 11
nc 1
nop 3
1
<?php
2
3
namespace NuvoleWeb\Drupal\Driver\Cores;
4
5
use function bovigo\assert\assert;
6
use function bovigo\assert\predicate\isNotEmpty;
7
use function bovigo\assert\predicate\isNotEqualTo;
8
use Drupal\Driver\Cores\Drupal8 as OriginalDrupal8;
9
use Drupal\node\Entity\Node;
10
use Drupal\node\Entity\NodeType;
11
use Drupal\taxonomy\Entity\Vocabulary;
12
13
/**
14
 * Class Drupal8.
15
 *
16
 * @package NuvoleWeb\Drupal\Driver\Cores
17
 */
18
class Drupal8 extends OriginalDrupal8 implements CoreInterface {
19
20
  /**
21
   * {@inheritdoc}
22
   */
23 View Code Duplication
  public function convertLabelToNodeTypeId($type) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    // First suppose that the id has been passed.
25
    if (NodeType::load($type)) {
26
      return $type;
27
    }
28
    $storage = \Drupal::entityTypeManager()->getStorage('node_type');
29
    $result = $storage->loadByProperties(['name' => $type]);
30
    assert($result, isNotEmpty());
31
    return key($result);
32
  }
33
34
  /**
35
   * {@inheritdoc}
36
   */
37 View Code Duplication
  public function convertLabelToTermTypeId($type) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    // First suppose that the id has been passed.
39
    if (Vocabulary::load($type)) {
40
      return $type;
41
    }
42
    $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary');
43
    $result = $storage->loadByProperties(['name' => $type]);
44
    assert($result, isNotEmpty());
45
    return key($result);
46
  }
47
48
  /**
49
   * {@inheritdoc}
50
   */
51
  public function loadNodeByName($title) {
52
    $result = \Drupal::entityQuery('node')
53
      ->condition('title', $title)
54
      ->condition('status', NODE_PUBLISHED)
55
      ->range(0, 1)
56
      ->execute();
57
    assert($result, isNotEmpty());
58
    $nid = current($result);
59
    return Node::load($nid);
60
  }
61
62
  /**
63
   * {@inheritdoc}
64
   */
65
  public function getEntityIdByLabel($entity_type, $bundle, $label) {
66
    /** @var NodeStorage $storage */
67
    $storage = \Drupal::entityTypeManager()->getStorage($entity_type);
68
    $bundle_key = $storage->getEntityType()->getKey('bundle');
69
    $label_key = $storage->getEntityType()->getKey('label');
70
    $result = \Drupal::entityQuery($entity_type)
71
      ->condition($bundle_key, $bundle)
72
      ->condition($label_key, $label)
73
      ->range(0, 1)
74
      ->execute();
75
    assert($result, isNotEmpty());
76
    return current($result);
77
  }
78
79
  /**
80
   * {@inheritdoc}
81
   */
82
  public function loadUserByName($name) {
83
    $user = user_load_by_name($name);
84
    assert($user, isNotEqualTo(FALSE));
85
    return $user;
86
  }
87
88
}
89