Completed
Pull Request — master (#16)
by Sergii
03:45
created

EntityDrupalWrapper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 106
ccs 0
cts 23
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 4
A load() 0 8 2
A hasField() 0 7 1
A getFieldValue() 0 7 1
A getFieldNameByLocator() 0 4 2
A getRequiredFields() 0 4 1
A getEntity() 0 8 2
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Utils;
6
7
final class EntityDrupalWrapper
8
{
9
    /**
10
     * Entity type.
11
     *
12
     * @var string
13
     */
14
    private $type = '';
15
    /**
16
     * Entity bundle.
17
     *
18
     * @var string
19
     */
20
    private $bundle = '';
21
    /**
22
     * Entity object.
23
     *
24
     * @var object
25
     */
26
    private $entity;
27
    /**
28
     * @var array
29
     */
30
    private $fields = [
31
        'locators' => [],
32
        'required' => [],
33
    ];
34
35
    /**
36
     * @param string $entityType
37
     * @param string $bundle
38
     */
39
    public function __construct($entityType, $bundle = '')
40
    {
41
        $this->type = $entityType;
42
43
        if (empty($bundle)) {
44
            $this->bundle = $this->type;
45
        }
46
47
        // The fields in "locators" array stored by machine name of a field and duplicated by field label.
48
        foreach (\DrupalKernelPlaceholder::getFieldDefinitions($this->type, $this->bundle) as $name => $definition) {
49
            $this->fields['locators'][$definition['label']] = $this->fields['locators'][$name] = $name;
50
51
            if ($definition['required']) {
52
                $this->fields['required'][$name] = $definition['label'];
53
            }
54
        }
55
    }
56
57
    public function load($id)
58
    {
59
        if (null === $this->entity) {
60
            $this->entity = \DrupalKernelPlaceholder::entityLoad($this->type, $id);
61
        }
62
63
        return $this->entity;
64
    }
65
66
    public function hasField($fieldName)
67
    {
68
        return \DrupalKernelPlaceholder::entityHasField(
69
            $this->getEntity(),
70
            $this->getFieldNameByLocator($fieldName)
71
        );
72
    }
73
74
    public function getFieldValue($fieldName)
75
    {
76
        return \DrupalKernelPlaceholder::entityFieldValue(
77
            $this->getEntity(),
78
            $this->getFieldNameByLocator($fieldName)
79
        );
80
    }
81
82
    /**
83
     * @param string $field_name
84
     *   Machine name or label of a field.
85
     *
86
     * @return string
87
     */
88
    public function getFieldNameByLocator($field_name)
89
    {
90
        return isset($this->fields['locators'][$field_name]) ? $this->fields['locators'][$field_name] : '';
91
    }
92
93
    /**
94
     * @return array[]
95
     */
96
    public function getRequiredFields()
97
    {
98
        return $this->fields['required'];
99
    }
100
101
    /**
102
     * @return object
103
     */
104
    protected function getEntity()
105
    {
106
        if (null === $this->entity) {
107
            throw new \RuntimeException('You have to load an entity before getting it.');
108
        }
109
110
        return $this->entity;
111
    }
112
}
113