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