EditBufferCache::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\paragraphs_editor\EditBuffer;
4
5
use Drupal\Core\Entity\EntityInterface;
6
use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface;
7
use Drupal\Core\Session\AccountInterface;
8
9
/**
10
 * Stores edit buffers in a persistent cache.
11
 */
12
class EditBufferCache implements EditBufferCacheInterface {
13
14
  /**
15
   * The back end storage mechanism for storing edit buffers.
16
   *
17
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
18
   */
19
  protected $storage;
20
21
  /**
22
   * The expiration time to set on cached buffers.
23
   *
24
   * @var int
25
   */
26
  protected $expiry;
27
28
  /**
29
   * The current user.
30
   *
31
   * @var \Drupal\Core\Session\AccountInterface
32
   */
33
  protected $user;
34
35
  /**
36
   * A list of buffer ids to be deleted from cache.
37
   *
38
   * @var array
39
   */
40
  protected $deleteQueue = [];
41
42
  /**
43
   * Creates an edit buffer cache object.
44
   *
45
   * @param \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $keyvalue_factory
46
   *   The key value factory service for creating a persistent cache.
47
   * @param int $expiry
48
   *   The amount of time to allow buffers to live without being automatically
49
   *   destroyed.
50
   * @param \Drupal\Core\Session\AccountInterface $user
51
   *   The current user.
52
   */
53 18
  public function __construct(KeyValueExpirableFactoryInterface $keyvalue_factory, $expiry, AccountInterface $user) {
54 18
    $this->storage = $keyvalue_factory->get('paragraphs_editor.edit_buffer');
55 18
    $this->expiry = $expiry;
56 18
    $this->user = $user;
57 18
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function get($context_string) {
63
64
    // If there is an existing buffer that doesn't have the correct context or
65
    // the buffer was created by a different user, we don't allow the caller to
66
    // access it.
67
    $buffer = $this->storage->get($context_string);
68
    if ($buffer) {
69
      if ($buffer->getContextString() != $context_string || $buffer->getUser() != $this->user->id()) {
70
        $buffer = NULL;
71
      }
72
    }
73
74
    // If we couldn't find a "good" buffer, we create a new one.
75
    if (!$buffer) {
76
      $buffer = new EditBuffer($context_string, $this->user->id());
77
    }
78
79
    // Tell the buffer about the cache so it can perform CRUD operations.
80
    $buffer->setCache($this);
81
82
    return $buffer;
83
  }
84
85
  /**
86
   * {@inheritdoc}
87
   */
88
  public function delete($context_string) {
89
    $buffer = $this->get($context_string);
90
    foreach ($buffer->getChildBufferTags() as $child_cache_key) {
91
      $this->delete($child_cache_key);
92
    }
93
    $this->storage->delete($context_string);
94
  }
95
96
  /**
97
   * {@inheritdoc}
98
   */
99
  public function save(EditBufferInterface $buffer) {
100
    $this->storage->setWithExpire($buffer->getContextString(), $buffer, $this->expiry);
101
102
    $parent_cache_key = $buffer->getParentBufferTag();
103
    if ($parent_cache_key) {
104
      $parent_buffer = $this->get($parent_cache_key);
105
      $parent_buffer->addChildBufferTag($buffer->getContextString());
106
      $this->save($parent_buffer);
107
    }
108
  }
109
110
  /**
111
   * {@inheritdoc}
112
   */
113 18
  public function processDeletionQueue(EntityInterface $entity) {
114 18
    $key = $this->deletionQueueKey($entity);
115 18
    if (!empty($this->deleteQueue[$key])) {
116
      foreach ($this->deleteQueue[$key] as $id) {
117
        $this->delete($id);
118
      }
119
    }
120 18
  }
121
122
  /**
123
   * {@inheritdoc}
124
   */
125
  public function queueDeletion(EntityInterface $entity, EditBufferInterface $buffer) {
126
    $this->deleteQueue[$this->deletionQueueKey($entity)][] = $buffer->getContextString();
127
  }
128
129
  /**
130
   * Helper function for generating a queue tag for an entity.
131
   *
132
   * @param \Drupal\Core\Entity\EntityInterface $entity
133
   *   The entity to create the tg for.
134
   *
135
   * @return string
136
   *   The created tag.
137
   */
138 18
  protected function deletionQueueKey(EntityInterface $entity) {
139 18
    return $entity->getEntityTypeId() . ':' . $entity->uuid();
140
  }
141
142
}
143