Completed
Pull Request — 8.x-3.x (#481)
by Sebastian
05:33
created

BufferBase::createResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Buffers;
4
5
/**
6
 * Base class for field buffering services.
7
 */
8
abstract class BufferBase {
9
10
  /**
11
   * The the array of buffers.
12
   *
13
   * @var \SplObjectStorage[]
14
   */
15
  protected $buffers = [];
16
17
  /**
18
   * The array of result sets.
19
   *
20
   * @var \SplObjectStorage[]
21
   */
22
  protected $results = [];
23
24
  /**
25
   * @param object $item
26
   *   The item to get the buffer id for.
27
   * @return string
28
   *   The buffer id.
29
   */
30
  protected function getBufferId($item) {
31
    return "";
32
  }
33
34
  /**
35
   * Helper function to create a resolver for a singular buffer.
36
   *
37
   * @param object $item
38
   *   The item to add to the buffer.
39
   *
40
   * @return \Closure
41
   *   The callback to invoke to load the result for this buffer item.
42
   */
43
  protected function createBufferResolver($item) {
44
    $bufferId = $this->getBufferId($item);
45
    if (!isset($this->buffers[$bufferId])) {
46
      $this->buffers[$bufferId] = new \SplObjectStorage();
47
    }
48
49
    if (!isset($this->results[$bufferId])) {
50
      $this->results[$bufferId] = new \SplObjectStorage();
51
    }
52
53
    // Add the created item to the buffer.
54
    $this->buffers[$bufferId]->attach($item, $item);
55
56
    // Return a callback that can be used to resolve the buffer item.
57
    return $this->createResolver($item, $this->buffers[$bufferId], $this->results[$bufferId]);
58
  }
59
60
  /**
61
   * Creates a callback to invoke to load the result for this buffer item.
62
   *
63
   * @param object $item
64
   *   The item to add to create the resolver for.
65
   * @param \SplObjectStorage $buffer
66
   *   The buffer.
67
   * @param \SplObjectStorage $result
68
   *   The result set.
69
   *
70
   * @return \Closure
71
   *   The callback to invoke to load the result for this buffer item.
72
   */
73
  protected function createResolver($item, \SplObjectStorage $buffer, \SplObjectStorage $result) {
74
    // Return the closure that will resolve and return the result for the item.
75
    return function () use ($item, $buffer, $result) {
76
      return $this->resolveItem($item, $buffer, $result);
77
    };
78
  }
79
80
  /**
81
   * Returns the result of the given item after processing the buffer if needed.
82
   *
83
   * @param object $item
84
   *   The buffer item to retrieve the result for.
85
   * @param \SplObjectStorage $buffer
86
   *   The buffer.
87
   * @param \SplObjectStorage $result
88
   *   The result set.
89
   *
90
   * @return mixed
91
   *   The result of resolving the given buffer item.
92
   */
93
  protected function resolveItem($item, \SplObjectStorage $buffer, \SplObjectStorage $result) {
94
    if ($buffer->contains($item)) {
95
      $results = $this->resolveBuffer($buffer);
96
97
      // Remove the resolved items from the buffer and add them to the results.
98
      $buffer->removeAll($results);
99
      $result->addAll($results);
100
    }
101
102
    if ($result->contains($item)) {
103
      return $result[$item];
104
    }
105
106
    throw new \LogicException('Failed to resolve item.');
107
  }
108
109
  /**
110
   * Resolves the given buffer wholly.
111
   *
112
   * @param \SplObjectStorage $buffer
113
   *   The buffer to be resolved wholly.
114
   *
115
   * @return \SplObjectStorage
116
   *   The resolved results for the given buffer, keyed by the corresponding
117
   *   buffer items.
118
   */
119
  protected function resolveBuffer(\SplObjectStorage $buffer) {
120
    // Convert the buffer to an array that we can later use to map the results
121
    // to the correct batch items.
122
    $buffer = iterator_to_array($buffer, FALSE);
123
124
    // Assign the loaded items to their corresponding batch items.
125
    $output = new \SplObjectStorage();
126
    foreach ($this->resolveBufferArray($buffer) as $key => $item) {
127
      $output->attach($buffer[$key], $item);
128
    }
129
130
    return $output;
131
  }
132
133
  /**
134
   * Resolve the buffer as an array.
135
   *
136
   * Simplifies sub-class implementations by concealing the object storage
137
   * details of the buffer object.
138
   *
139
   * @param array $buffer
140
   *   The buffer as an array.
141
   *
142
   * @return array
143
   *   The resolved results, keyed by their corresponding buffer item array key.
144
   */
145
  protected function resolveBufferArray(array $buffer) {
146
    throw new \LogicException('Method not implemented.');
147
  }
148
}