Completed
Pull Request — master (#157)
by
unknown
01:41
created

DriverEntityPluginDrupal8Base   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 3
dl 0
loc 260
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A delete() 0 4 1
A getBundleKey() 0 6 1
A getBundleKeyLabels() 0 11 2
A getBundles() 0 10 2
A getEntity() 0 8 2
A getLabelKeys() 0 12 2
A id() 0 4 1
A isNew() 0 7 3
A label() 0 4 1
A load() 0 9 3
A reload() 0 10 2
A save() 0 5 1
A set() 0 7 2
A url() 0 4 1
A getNewDriverField() 0 12 1
A getStorage() 0 4 1
A getNewEntity() 0 12 2
1
<?php
2
3
namespace Drupal\Driver\Plugin;
4
5
use Drupal\Component\Plugin\PluginBase;
6
use Drupal\Driver\Exception\Exception;
7
use Drupal\Driver\Wrapper\Field\DriverFieldInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10
use Drupal\Core\Entity\EntityInterface;
11
use Drupal\Driver\Wrapper\Field\DriverFieldDrupal8;
12
use Drupal\Driver\Wrapper\Entity\DriverEntityInterface;
13
14
/**
15
 * Provides a base class for the Driver's entity plugins.
16
 */
17
class DriverEntityPluginDrupal8Base extends DriverEntityPluginBase implements DriverEntityPluginInterface, DriverEntityInterface
18
{
19
20
  /**
21
   * The id of the attached entity.
22
   *
23
   * @var integer|string;
24
   *
25
   * @deprecated Use id() instead.
26
   */
27
    public $id;
28
29
  /**
30
   * Entity type definition.
31
   *
32
   * @var \Drupal\Core\Entity\EntityStorageInterface
33
   */
34
    protected $storage;
35
36
  /**
37
   * The saved Drupal entity this object is wrapping for the Driver.
38
   *
39
   * @var \Drupal\Core\Entity\EntityInterface;
40
   */
41
    protected $entity;
42
43
  /**
44
   * The driver field plugin manager.
45
   *
46
   * @var \Drupal\Driver\Plugin\DriverPluginManagerInterface;
47
   */
48
    protected $fieldPluginManager;
49
50
51
  /**
52
   * The drupal entity type manager.
53
   *
54
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface;
55
   */
56
    protected $entityTypeManager;
57
58
  /**
59
   * {@inheritdoc}
60
   */
61
    public function __construct(
62
        array $configuration,
63
        $plugin_id,
64
        $plugin_definition
65
    ) {
66
        parent::__construct($configuration, $plugin_id, $plugin_definition);
67
        $this->entityTypeManager = \Drupal::entityTypeManager();
68
        $this->storage = $this->entityTypeManager->getStorage($this->type);
69
    }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74
    public function delete()
75
    {
76
        $this->getEntity()->delete();
77
    }
78
79
  /**
80
   * {@inheritdoc}
81
   */
82
    public function getBundleKey()
83
    {
84
        return $this->entityTypeManager
85
        ->getDefinition($this->type)
86
        ->getKey('bundle');
87
    }
88
89
  /**
90
   * {@inheritdoc}
91
   */
92
    public function getBundleKeyLabels()
93
    {
94
        $bundleKeyLabel = null;
95
        $bundleKey = $this->getBundleKey();
96
        if (!empty($bundleKey)) {
97
            $definitions = \Drupal::service('entity_field.manager')
98
            ->getBaseFieldDefinitions($this->type);
99
            $bundleKeyLabel = $definitions[$bundleKey]->getLabel();
100
        }
101
        return [(string) $bundleKeyLabel];
102
    }
103
104
  /**
105
   * {@inheritdoc}
106
   */
107
    public function getBundles()
108
    {
109
        $bundleInfo = \Drupal::service('entity_type.bundle.info')->getBundleInfo($this->type);
110
        // Parse into array structure used by DriverNameMatcher.
111
        $bundles = [];
112
        foreach ($bundleInfo as $machineName => $bundleSettings) {
113
            $bundles[$bundleSettings['label']] = $machineName;
114
        }
115
        return $bundles;
116
    }
117
118
  /**
119
   * {@inheritdoc}
120
   */
121
    public function getEntity()
122
    {
123
        $entity = parent::getEntity();
124
        if (!$entity instanceof EntityInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Entity\EntityInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
125
            throw new \Exception("Failed to obtain valid entity");
126
        }
127
        return $this->entity;
128
    }
129
130
  /**
131
   * {@inheritdoc}
132
   */
133
    public function getLabelKeys()
134
    {
135
        $labelKeys = parent::getLabelKeys();
136
        if (empty($labelKeys)) {
137
            $labelKeys = [
138
            $this->entityTypeManager
139
              ->getDefinition($this->type)
140
              ->getKey('label')
141
            ];
142
        }
143
        return $labelKeys;
144
    }
145
146
  /**
147
   * {@inheritdoc}
148
   */
149
    public function id()
150
    {
151
        return $this->getEntity()->id();
152
    }
153
154
  /**
155
   * {@inheritdoc}
156
   */
157
    public function isNew()
158
    {
159
        if ($this->hasEntity() && !$this->entity->isNew()) {
160
            return false;
161
        }
162
        return true;
163
    }
164
165
  /**
166
   * {@inheritdoc}
167
   */
168
    public function label()
169
    {
170
        return $this->getEntity()->label();
171
    }
172
173
  /**
174
   * {@inheritdoc}
175
   */
176
    public function load($entityId)
177
    {
178
        if ($this->hasEntity()) {
179
            throw new \Exception("A Drupal entity is already attached to this plugin");
180
        }
181
        $this->entity = $this->getStorage()->load($entityId);
182
        $this->id = is_null($this->entity) ? NULL : $this->id();
0 ignored issues
show
Deprecated Code introduced by
The property Drupal\Driver\Plugin\Dri...yPluginDrupal8Base::$id has been deprecated with message: Use id() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
183
        return $this->entity;
184
    }
185
186
  /**
187
   * {@inheritdoc}
188
   */
189
    public function reload()
190
    {
191
        if (!$this->hasEntity()) {
192
            throw new \Exception("There is no attached entity so it cannot be reloaded");
193
        }
194
        $entityId = $this->getEntity()->id();
195
        $this->getStorage()->resetCache([$entityId]);
196
        $this->entity = $this->getStorage()->load($entityId);
197
        return $this->entity;
198
    }
199
200
  /**
201
   * {@inheritdoc}
202
   */
203
    public function save()
204
    {
205
        $this->getEntity()->save();
206
        $this->id = $this->id();
0 ignored issues
show
Deprecated Code introduced by
The property Drupal\Driver\Plugin\Dri...yPluginDrupal8Base::$id has been deprecated with message: Use id() instead.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
207
    }
208
209
  /**
210
   * {@inheritdoc}
211
   */
212
    public function set($identifier, $field)
213
    {
214
        if (!($field instanceof DriverFieldInterface)) {
215
            $field = $this->getNewDriverField($identifier, $field);
216
        }
217
        $this->getEntity()->set($field->getName(), $field->getProcessedValues());
218
    }
219
220
  /**
221
   * {@inheritdoc}
222
   */
223
    public function url($rel = 'canonical', $options = [])
224
    {
225
        return $this->getEntity()->url($rel, $options);
226
    }
227
228
  /**
229
   * Get a new driver field with values.
230
   *
231
   * @param string $fieldName
232
   *   A string identifying an entity field.
233
   * @param string|array $values
234
   *   An input that can be transformed into Driver field values.
235
   */
236
    protected function getNewDriverField($fieldName, $values)
237
    {
238
        $field = new DriverFieldDrupal8(
239
            $values,
240
            $fieldName,
241
            $this->type,
242
            $this->bundle,
243
            $this->projectPluginRoot,
244
            $this->fieldPluginManager
245
        );
246
        return $field;
247
    }
248
249
  /**
250
   * Get the entity type storage.
251
   *
252
   * @return \Drupal\Core\Entity\EntityStorageInterface
253
   */
254
    protected function getStorage()
255
    {
256
        return $this->storage;
257
    }
258
259
  /**
260
   * Get a new entity object.
261
   *
262
   * @return \Drupal\Core\Entity\EntityInterface
263
   */
264
    protected function getNewEntity()
265
    {
266
        $values = [];
267
        // Set the bundle as a field if not simply using the default for
268
        // a bundle-less entity type.
269
        if ($this->type !== $this->bundle) {
270
            $bundleKey = $this->getBundleKey();
271
            $values[$bundleKey] = $this->bundle;
272
        }
273
        $entity = $this->getStorage()->create($values);
274
        return $entity;
275
    }
276
}
277