summaryrefslogtreecommitdiffstats
path: root/src/Entity/ConfigSnapshot.php
blob: a5c6f2858f000026cb9de069c19c5fc4856b86ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php

namespace Drupal\config_snapshot\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;

/**
 * Defines the Config snapshot entity.
 *
 * @ConfigEntityType(
 *   id = "config_snapshot",
 *   label = @Translation("Config snapshot"),
 *   label_singular = @Translation("Config snapshot item"),
 *   label_plural = @Translation("Config snapshot items"),
 *   label_count = @PluralTranslation(
 *     singular = "@count config snapshot item",
 *     plural = "@count config snapshot items",
 *   ),
 *   config_prefix = "snapshot",
 *   admin_permission = "administer site configuration",
 *   entity_keys = {
 *     "id" = "id"
 *   },
 *   config_export = {
 *     "id",
 *     "snapshotSet",
 *     "extensionType",
 *     "extensionName",
 *     "items"
 *   }
 * )
 */
class ConfigSnapshot extends ConfigEntityBase implements ConfigSnapshotInterface {

  /**
   * The snapshot ID.
   *
   * @var string
   */
  protected $id;

  /**
   * The snapshot set.
   *
   * A set is a group of snapshots used for a particular purpose. A set should
   * be namespaced for the module introducing it.
   *
   * @var string
   */
  protected $snapshotSet;

  /**
   * The snapshot type.
   *
   * @var string
   */
  protected $extensionType;

  /**
   * The snapshot name.
   *
   * @var string
   */
  protected $extensionName;

  /**
   * The snapshot items.
   *
   * @var array[]
   */
  protected $items = [];

  /**
   * {@inheritdoc}
   */
  public function id() {
    return "{$this->snapshotSet}.{$this->extensionType}.{$this->extensionName}";
  }

  /**
   * {@inheritdoc}
   */
  public function getSnapshotSet() {
    return $this->snapshotSet;
  }

  /**
   * {@inheritdoc}
   */
  public function getExtensionType() {
    return $this->extensionType;
  }

  /**
   * {@inheritdoc}
   */
  public function getExtensionName() {
    return $this->extensionName;
  }

  /**
   * {@inheritdoc}
   */
  public function getItems() {
    return $this->items;
  }

  /**
   * {@inheritdoc}
   */
  public function setItems(array $items) {
    $this->items = $items;

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getItem($collection, $name) {
    if (($key = $this->getItemKey($collection, $name)) !== FALSE) {
      return $this->items[$key];
    }

    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setItem($collection, $name, $data) {
    $item = [
      'collection' => $collection,
      'name' => $name,
      'data' => $data,
    ];
    if (($key = $this->getItemKey($collection, $name)) !== FALSE) {
      $this->items[$key] = $item;
    }
    else {
      $this->items[] = $item;
    }

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function clearItem($collection, $name) {
    if (($key = $this->getItemKey($collection, $name)) !== FALSE) {
      unset($this->items[$key]);
    }

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function save() {
    $collections = array_column($this->items, 'collection');
    $names = array_column($this->items, 'name');
    // Sort the items by collection then by name.
    array_multisort($collections, SORT_ASC, $names, SORT_ASC, $this->items);

    parent::save();
  }

  /**
   * Returns the key of a given item.
   *
   * @return int|FALSE
   *   The key of the item or FALSE if not found.
   */
  protected function getItemKey($collection, $name) {
    $items = array_filter($this->items, function ($item) use ($collection, $name) {
      return ($item['collection'] === $collection) && ($item['name'] === $name);
    });
    if ($items) {
      return key($items);
    }

    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();

    // Create a dependency on the extension.
    $this->addDependency($this->extensionType, $this->extensionName);

    return $this;
  }

}