EmbedButton   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 185
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeId() 0 3 1
A getTypeLabel() 0 8 2
A getTypePlugin() 0 5 2
A getIconFile() 0 5 2
A getIconUrl() 0 8 2
A calculateDependencies() 0 14 3
A embedTypeManager() 0 3 1
A fileUsage() 0 3 1
B postSave() 0 18 7
A postDelete() 0 11 3
A getTypeSetting() 0 8 2
A getTypeSettings() 0 3 1
1
<?php
2
3
namespace Drupal\embed\Entity;
4
5
use Drupal\Core\Config\Entity\ConfigEntityBase;
6
use Drupal\Core\Entity\EntityStorageInterface;
7
use Drupal\Core\StringTranslation\StringTranslationTrait;
8
use Drupal\embed\EmbedButtonInterface;
9
10
/**
11
 * Defines the EmbedButton entity.
12
 *
13
 * @ConfigEntityType(
14
 *   id = "embed_button",
15
 *   label = @Translation("Embed button"),
16
 *   handlers = {
17
 *     "form" = {
18
 *       "add" = "Drupal\embed\Form\EmbedButtonForm",
19
 *       "edit" = "Drupal\embed\Form\EmbedButtonForm",
20
 *       "delete" = "Drupal\Core\Entity\EntityDeleteForm",
21
 *     },
22
 *     "list_builder" = "Drupal\embed\EmbedButtonListBuilder",
23
 *   },
24
 *   admin_permission = "administer embed buttons",
25
 *   config_prefix = "button",
26
 *   entity_keys = {
27
 *     "id" = "id",
28
 *     "label" = "label",
29
 *   },
30
 *   links = {
31
 *     "edit-form" = "/admin/config/content/embed/button/manage/{embed_button}",
32
 *     "delete-form" = "/admin/config/content/embed/button/manage/{embed_button}/delete",
33
 *     "collection" = "/admin/config/content/embed",
34
 *   },
35
 *   config_export = {
36
 *     "label",
37
 *     "id",
38
 *     "type_id",
39
 *     "type_settings",
40
 *     "icon_uuid",
41
 *   }
42
 * )
43
 */
44
class EmbedButton extends ConfigEntityBase implements EmbedButtonInterface {
45
46
  use StringTranslationTrait;
47
48
  /**
49
   * The EmbedButton ID.
50
   *
51
   * @var string
52
   */
53
  public $id;
54
55
  /**
56
   * Label of EmbedButton.
57
   *
58
   * @var string
59
   */
60
  public $label;
61
62
  /**
63
   * The embed type plugin ID.
64
   *
65
   * @var string
66
   */
67
  public $type_id;
68
69
  /**
70
   * Embed type settings.
71
   *
72
   * An array of key/value pairs.
73
   *
74
   * @var array
75
   */
76
  public $type_settings = [];
77
78
  /**
79
   * UUID of the button's icon file.
80
   *
81
   * @var string
82
   */
83
  public $icon_uuid;
84
85
  /**
86
   * {@inheritdoc}
87
   */
88
  public function getTypeId() {
89
    return $this->type_id;
90
  }
91
92
  /**
93
   * {@inheritdoc}
94
   */
95
  public function getTypeLabel() {
96
    if ($definition = $this->embedTypeManager()->getDefinition($this->getTypeId(), FALSE)) {
97
      return $definition['label'];
98
    }
99
    else {
100
      return $this->t('Unknown');
101
    }
102
  }
103
104
  /**
105
   * {@inheritdoc}
106
   */
107
  public function getTypePlugin() {
108
    if ($plugin_id = $this->getTypeId()) {
109
      return $this->embedTypeManager()->createInstance($plugin_id, $this->getTypeSettings());
110
    }
111
  }
112
113
  /**
114
   * {@inheritdoc}
115
   */
116
  public function getIconFile() {
117
    if ($this->icon_uuid) {
118
      return $this->entityManager()->loadEntityByUuid('file', $this->icon_uuid);
119
    }
120
  }
121
122
  /**
123
   * {@inheritdoc}
124
   */
125
  public function getIconUrl() {
126
    if ($image = $this->getIconFile()) {
127
      return file_create_url($image->getFileUri());
128
    }
129
    else {
130
      return $this->getTypePlugin()->getDefaultIconUrl();
131
    }
132
  }
133
134
  /**
135
   * {@inheritdoc}
136
   */
137
  public function calculateDependencies() {
138
    parent::calculateDependencies();
139
140
    // Add the file icon entity as dependency if an UUID was specified.
141
    if ($this->icon_uuid && $file_icon = $this->entityManager()->loadEntityByUuid('file', $this->icon_uuid)) {
142
      $this->addDependency($file_icon->getConfigDependencyKey(), $file_icon->getConfigDependencyName());
143
    }
144
145
    // Gather the dependencies of the embed type plugin.
146
    $plugin = $this->getTypePlugin();
147
    $this->calculatePluginDependencies($plugin);
148
149
    return $this->dependencies;
150
  }
151
152
  /**
153
   * Gets the embed type plugin manager.
154
   *
155
   * @return \Drupal\embed\EmbedType\EmbedTypeManager
156
   *   The embed type plugin manager.
157
   */
158
  protected function embedTypeManager() {
159
    return \Drupal::service('plugin.manager.embed.type');
160
  }
161
162
  /**
163
   * Gets the file usage service.
164
   *
165
   * @return \Drupal\file\FileUsage\FileUsageInterface
166
   *   The file usage service.
167
   */
168
  protected function fileUsage() {
169
    return \Drupal::service('file.usage');
170
  }
171
172
  /**
173
   * {@inheritdoc}
174
   */
175
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
176
    parent::postSave($storage, $update);
177
178
    $icon_file = $this->getIconFile();
179
    if (isset($this->original) && $old_icon_file = $this->original->getIconFile()) {
180
      /** @var \Drupal\file\FileInterface $old_icon_file */
181
      if (!$icon_file || $icon_file->uuid() != $old_icon_file->uuid()) {
182
        $this->fileUsage()->delete($old_icon_file, 'embed', $this->getEntityTypeId(), $this->id());
183
      }
184
    }
185
186
    if ($icon_file) {
187
      $usage = $this->fileUsage()->listUsage($icon_file);
188
      if (empty($usage['embed'][$this->getEntityTypeId()][$this->id()])) {
189
        $this->fileUsage()->add($icon_file, 'embed', $this->getEntityTypeId(), $this->id());
190
      }
191
    }
192
  }
193
194
  /**
195
   * {@inheritdoc}
196
   */
197
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
198
    parent::postDelete($storage, $entities);
199
200
    // Remove file usage for any button icons.
201
    foreach ($entities as $entity) {
202
      /** @var \Drupal\embed\EmbedButtonInterface $entity */
203
      if ($icon_file = $entity->getIconFile()) {
204
        \Drupal::service('file.usage')->delete($icon_file, 'embed', $entity->getEntityTypeId(), $entity->id());
205
      }
206
    }
207
  }
208
209
  /**
210
   * {@inheritdoc}
211
   */
212
  public function getTypeSetting($key, $default = NULL) {
213
    if (isset($this->type_settings[$key])) {
214
      return $this->type_settings[$key];
215
    }
216
    else {
217
      return $default;
218
    }
219
  }
220
221
  /**
222
   * {@inheritdoc}
223
   */
224
  public function getTypeSettings() {
225
    return $this->type_settings;
226
  }
227
228
}
229