EditBuffer::getItems()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 1
dl 0
loc 19
ccs 0
cts 17
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\paragraphs_editor\EditBuffer;
4
5
use Drupal\paragraphs\ParagraphInterface;
6
7
/**
8
 * Stores edits made in the editor.
9
 */
10
class EditBuffer implements EditBufferInterface {
11
12
  /**
13
   * The id of the context this buffer belongs to.
14
   *
15
   * @var string
16
   */
17
  protected $contextString;
18
19
  /**
20
   * The id of the user who owns this edit buffer.
21
   *
22
   * @var int
23
   */
24
  protected $uid;
25
26
  /**
27
   * The id of the context this buffer's context is nested inside.
28
   *
29
   * @var string
30
   */
31
  protected $parentBufferTag = NULL;
32
33
  /**
34
   * The cache service for saving the edit buffer.
35
   *
36
   * @var \Drupal\paragraphs_editor\EditBuffer\EditBufferCacheInterface
37
   */
38
  protected $bufferCache = NULL;
39
40
  /**
41
   * A list of entities in this buffer.
42
   *
43
   * @var \Drupal\paragraphs\ParagraphInterface[]
44
   */
45
  protected $paragraphs = [];
46
47
  /**
48
   * A list of context ids for contexts nested inside this buffer's context.
49
   *
50
   * @var string[]
51
   */
52
  protected $childBufferTags = [];
53
54
  /**
55
   * Creates an edit buffer object.
56
   *
57
   * @param string $context_string
58
   *   The id of the context this edit buffer belongs to.
59
   * @param int $uid
60
   *   The user entity id that owns this buffer.
61
   */
62
  public function __construct($context_string, $uid) {
63
    $this->contextString = $context_string;
64
    $this->uid = $uid;
65
  }
66
67
  /**
68
   * {@inheritdoc}
69
   */
70
  public function getUser() {
71
    return $this->uid;
72
  }
73
74
  /**
75
   * {@inheritdoc}
76
   */
77
  public function getContextString() {
78
    return $this->contextString;
79
  }
80
81
  /**
82
   * {@inheritdoc}
83
   */
84
  public function setItem(EditBufferItemInterface $item) {
85
    $uuid = $item->getEntity()->uuid();
86
    $this->paragraphs[$uuid] = $item->getEntity();
87
    return $this;
88
  }
89
90
  /**
91
   * {@inheritdoc}
92
   */
93
  public function getItem($paragraph_uuid) {
94
    $paragraph = isset($this->paragraphs[$paragraph_uuid]) ? $this->paragraphs[$paragraph_uuid] : NULL;
95
    if ($paragraph) {
96
      $item = $this->createEditBufferItem($paragraph);
97
    }
98
    else {
99
      $item = NULL;
100
    }
101
    return $item;
102
  }
103
104
  /**
105
   * {@inheritdoc}
106
   */
107
  public function getItems($bundle_name = NULL) {
108
    if ($bundle_name) {
109
      $paragraphs = [];
110
      foreach ($this->paragraphs as $paragraph) {
111
        if ($paragraph->bundle() == $bundle_name) {
112
          $paragraphs[$paragraph->uuid()] = $paragraph;
113
        }
114
      }
115
    }
116
    else {
117
      $paragraphs = $this->paragraphs;
118
    }
119
120
    $items = [];
121
    foreach ($paragraphs as $paragraph) {
122
      $items[$paragraph->uuid()] = $this->createEditBufferItem($paragraph);
123
    }
124
125
    return $items;
126
  }
127
128
  /**
129
   * {@inheritdoc}
130
   */
131
  public function createItem(ParagraphInterface $paragraph) {
132
    $item = new EditBufferItem($paragraph, $this);
133
    $this->paragraphs[$paragraph->uuid()] = $paragraph;
134
    return $item;
135
  }
136
137
  /**
138
   * {@inheritdoc}
139
   */
140
  public function setCache(EditBufferCacheInterface $buffer_cache) {
141
    $this->bufferCache = $buffer_cache;
142
  }
143
144
  /**
145
   * {@inheritdoc}
146
   */
147
  public function save() {
148
    $this->bufferCache->save($this);
149
  }
150
151
  /**
152
   * {@inheritdoc}
153
   */
154
  public function tagParentBuffer($context_string) {
155
    $this->parentBufferTag = $context_string;
156
  }
157
158
  /**
159
   * {@inheritdoc}
160
   */
161
  public function getParentBufferTag() {
162
    return $this->parentBufferTag;
163
  }
164
165
  /**
166
   * {@inheritdoc}
167
   */
168
  public function addChildBufferTag($context_string) {
169
    $this->childBufferTags[$context_string] = TRUE;
170
  }
171
172
  /**
173
   * {@inheritdoc}
174
   */
175
  public function getChildBufferTags() {
176
    return array_flip($this->childBufferTags);
177
  }
178
179
  /**
180
   * {@inheritdoc}
181
   */
182
  public function createCopy($context_string) {
183
    $copy = clone $this;
184
    $copy->contextString = $context_string;
185
    return $copy;
186
  }
187
188
  /**
189
   * {@inheritdoc}
190
   */
191
  public function toArray() {
192
    $properties = get_object_vars($this);
193
    unset($properties['bufferCache']);
194
    unset($properties['parentBufferTag']);
195
    return $properties;
196
  }
197
198
  /**
199
   * Gets the array keys to be serialized.
200
   *
201
   * @return array
202
   *   The keys to be serialized.
203
   */
204
  public function __sleep() {
205
    return array_keys($this->toArray());
206
  }
207
208
  /**
209
   * Restores the object when it is unserialized.
210
   */
211
  public function __wakeup() {
212
    $this->bufferCache = \Drupal::service('paragraphs_editor.edit_buffer.cache');
213
  }
214
215
  /**
216
   * Creates an edit buffer item object.
217
   *
218
   * @param \Drupal\paragraphs\ParagraphInterface $paragraph
219
   *   The paragraph to be wrapped in the item.
220
   *
221
   * @return \Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface
222
   *   The created edit buffer item.
223
   */
224
  protected function createEditBufferItem(ParagraphInterface $paragraph) {
225
    return new EditBufferItem($paragraph, $this);
226
  }
227
228
}
229