WidgetBinderDataCompilerState   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
dl 0
loc 129
ccs 17
cts 20
cp 0.85
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getItemContext() 0 2 1
A get() 0 6 3
A getCompiledData() 0 2 1
A set() 0 2 1
A getItem() 0 2 1
A __construct() 0 5 1
A getGenerator() 0 2 2
1
<?php
2
3
namespace Drupal\paragraphs_editor\WidgetBinder;
4
5
use Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface;
6
use Drupal\paragraphs_editor\EditorCommand\CommandContextInterface;
7
8
/**
9
 * Holds the state of the widget binder data compiler.
10
 *
11
 * Since each generator is defined as a service, state information cannot be
12
 * safely kept as a property of the generator itself. To allow generators to
13
 * track and aggregate progress, we introduce a state object.
14
 */
15
class WidgetBinderDataCompilerState {
16
17
  /**
18
   * Contains the state data that will be destroyed at the end of compilation.
19
   *
20
   * @var array
21
   */
22
  protected $temporaryData = [];
23
24
  /**
25
   * Contains the current compiled data.
26
   *
27
   * @var \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderData
28
   */
29
  protected $compiledData;
30
31
  /**
32
   * Contains the list of active generator service objects.
33
   *
34
   * @var \Drupal\paragraphs_editor\WidgetBinder\GeneratorInterface[]
35
   */
36
  protected $generators;
37
38
  /**
39
   * The context containing the edit buffer item currently being compiled.
40
   *
41
   * @var \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface
42
   */
43
  protected $itemContext;
44
45
  /**
46
   * The edit buffer item currently being compiled.
47
   *
48
   * @var \Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface
49
   */
50
  protected $item;
51
52
  /**
53
   * Creates a WidgetBinderDataCompilerState object.
54
   *
55
   * @param \Drupal\paragraphs_editor\WidgetBinder\GeneratorInterface[] $generators
56
   *   A key value map where keys are generator ids and values are generator
57
   *   instances that are active for the current compiler session.
58
   * @param \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderData $data
59
   *   The compiled data containing the models that will be delivered to the
60
   *   client.
61
   * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface $context
62
   *   The context containing the edit buffer item being compiled.
63
   * @param \Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface $item
64
   *   The edit buffer item being compiled.
65
   */
66 12
  public function __construct(array $generators, WidgetBinderData $data, CommandContextInterface $context, EditBufferItemInterface $item) {
67 12
    $this->compiledData = $data;
68 12
    $this->generators = $generators;
69 12
    $this->itemContext = $context;
70 12
    $this->item = $item;
71 12
  }
72
73
  /**
74
   * Getter for the compiled widget binder data.
75
   *
76
   * @return \Drupal\paragraphs_editor\WidgetBinder\WidgetBinderData
77
   *   The widget binder data to be delivered to the client.
78
   */
79
  public function getCompiledData() {
80
    return $this->compiledData;
81
  }
82
83
  /**
84
   * Getter for the context containing the item being edited.
85
   *
86
   * @return \Drupal\paragraphs_editor\EditorCommand\CommandContextInterface
87
   *   The context containing the item being edited.
88
   */
89 8
  public function getItemContext() {
90 8
    return $this->itemContext;
91
  }
92
93
  /**
94
   * Getter for the edit buffer item being compiled.
95
   *
96
   * @return \Drupal\paragraphs_editor\EditBuffer\EditBufferItemInterface
97
   *   The edit buffer item being compiled.
98
   */
99 6
  public function getItem() {
100 6
    return $this->item;
101
  }
102
103
  /**
104
   * Gets a generator by its generator id.
105
   *
106
   * @return \Drupal\paragraphs_editor\WidgetBinder\GeneratorInterface|null
107
   *   The generator service implementation assocaited with a generator id, or
108
   *   NULL if no such generator exists.
109
   */
110 2
  public function getGenerator($id) {
111 2
    return !empty($this->generators[$id]) ? $this->generators[$id] : NULL;
112
  }
113
114
  /**
115
   * Gets a key from the temporary state store.
116
   *
117
   * @param string|null $key
118
   *   A key entry to get the value for, or NULL to return an array of all
119
   *   values.
120
   *
121
   * @return mixed
122
   *   The value associated with the specified key, or an array of all key value
123
   *   pairs if NULL was passed.
124
   */
125 8
  public function get($key = NULL) {
126 8
    if (isset($key)) {
127 8
      return isset($this->temporaryData[$key]) ? $this->temporaryData[$key] : NULL;
128
    }
129
    else {
130
      return $this->temporaryData;
131
    }
132
  }
133
134
  /**
135
   * Sets a temporary state value.
136
   *
137
   * @param string $key
138
   *   The key of the temporary value to set.
139
   * @param mixed $value
140
   *   The value to store in the temporary state cache.
141
   */
142 8
  public function set($key, $value) {
143 8
    $this->temporaryData[$key] = $value;
144 8
  }
145
146
}
147