Passed
Push — master ( 0c0f23...09d191 )
by Juuso
02:11
created

DataLoader::scheduleDispatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leinonen\DataLoader;
6
7
use React\Promise\Promise;
8
use function React\Promise\all;
9
use function React\Promise\reject;
10
use React\EventLoop\LoopInterface;
11
use function React\Promise\resolve;
12
use React\Promise\ExtendedPromiseInterface;
13
14
final class DataLoader implements DataLoaderInterface
15
{
16
    /**
17
     * @var callable
18
     */
19
    private $batchLoadFunction;
20
21
    /**
22
     * @var array
23
     */
24
    private $promiseQueue = [];
25
26
    /**
27
     * @var CacheMapInterface
28
     */
29
    private $promiseCache;
30
31
    /**
32
     * @var LoopInterface
33
     */
34
    private $eventLoop;
35
36
    /**
37
     * @var DataLoaderOptions
38
     */
39
    private $options;
40
41
    /**
42
     * Initiates a new DataLoader.
43
     *
44
     * @param callable $batchLoadFunction The function which will be called for the batch loading.
45
     * It must accept an array of keys and returns a Promise which resolves to an array of values.
46
     * @param LoopInterface $loop
47
     * @param CacheMapInterface $cacheMap
48
     * @param null|DataLoaderOptions $options
49
     */
50 32
    public function __construct(
51
        callable $batchLoadFunction,
52
        LoopInterface $loop,
53
        CacheMapInterface $cacheMap,
54
        DataLoaderOptions $options = null
55
    ) {
56 32
        $this->batchLoadFunction = $batchLoadFunction;
57 32
        $this->eventLoop = $loop;
58 32
        $this->promiseCache = $cacheMap;
59 32
        $this->options = $options ?? new DataLoaderOptions();
60 32
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 32
    public function load($key): ExtendedPromiseInterface
66
    {
67 32
        if ($key === null) {
68 2
            throw new \InvalidArgumentException(self::class . '::load must be called with a value, but got null');
69
        }
70
71 30
        if ($this->options->shouldCache() && $this->promiseCache->get($key)) {
72 9
            return $this->promiseCache->get($key);
73
        }
74
75 29
        $promise = new Promise(
76
            function (callable $resolve, callable $reject) use ($key) {
77 29
                $this->promiseQueue[] = [
78 29
                    'key' => $key,
79 29
                    'resolve' => $resolve,
80 29
                    'reject' => $reject,
81
                ];
82
83 29
                if (\count($this->promiseQueue) === 1) {
84 29
                    $this->scheduleDispatch();
85
                }
86 29
            }
87
        );
88
89 29
        if ($this->options->shouldCache()) {
90 28
            $this->promiseCache->set($key, $promise);
91
        }
92
93 29
        return $promise;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 2
    public function loadMany(array $keys): ExtendedPromiseInterface
100
    {
101 2
        return all(
102 2
            \array_map(
103
                function ($key) {
104 2
                    return $this->load($key);
105 2
                },
106 2
                $keys
107
            )
108
        );
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 11
    public function clear($key): void
115
    {
116 11
        $this->promiseCache->delete($key);
117 11
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 1
    public function clearAll(): void
123
    {
124 1
        $this->promiseCache->clear();
125 1
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 4
    public function prime($key, $value): void
131
    {
132 4
        if (! $this->promiseCache->get($key)) {
133
            // Cache a rejected promise if the value is an Exception, in order to match
134
            // the behavior of load($key).
135 4
            $promise = $value instanceof \Exception ? reject($value) : resolve($value);
136
137 4
            $this->promiseCache->set($key, $promise);
138
        }
139 4
    }
140
141
    /**
142
     * Schedules the dispatch to happen on the next tick of the EventLoop
143
     * If batching is disabled, schedule the dispatch immediately.
144
     *
145
     * @return void
146
     */
147 29
    private function scheduleDispatch(): void
148
    {
149 29
        if ($this->options->shouldBatch()) {
150 28
            $this->eventLoop->futureTick(
151
                function () {
152 27
                    $this->dispatchQueue();
153 28
                }
154
            );
155
156 28
            return;
157
        }
158
159 1
        $this->dispatchQueue();
160 1
    }
161
162
    /**
163
     * Resets and dispatches the DataLoaders queue.
164
     *
165
     * @return void
166
     */
167 28
    private function dispatchQueue(): void
168
    {
169 28
        $queue = $this->promiseQueue;
170 28
        $this->promiseQueue = [];
171
172 28
        $maxBatchSize = $this->options->getMaxBatchSize();
173
174 28
        if ($maxBatchSize !== null && $maxBatchSize > 0 && $maxBatchSize < count($queue)) {
175 1
            $this->dispatchQueueInMultipleBatches($queue, $maxBatchSize);
176
        } else {
177 27
            $this->dispatchQueueBatch($queue);
178
        }
179 28
    }
180
181
    /**
182
     * Dispatches a batch of a queue. The given batch can also be the whole queue.
183
     *
184
     * @param array $batch
185
     */
186 28
    private function dispatchQueueBatch($batch)
187
    {
188 28
        $keys = \array_column($batch, 'key');
189 28
        $batchLoadFunction = $this->batchLoadFunction;
190
191
        /** @var Promise $batchPromise */
192 28
        $batchPromise = $batchLoadFunction($keys);
193
194
        try {
195 28
            $this->validateBatchPromise($batchPromise);
196 4
        } catch (DataLoaderException $exception) {
197 4
            return $this->handleFailedDispatch($batch, $exception);
198
        }
199
200 24
        $batchPromise->then(
201
            function ($values) use ($batch, $keys) {
202 23
                $this->validateBatchPromiseOutput($values, $keys);
203 21
                $this->handleSuccessfulDispatch($batch, $values);
204 24
            }
205
        )->then(null, function ($error) use ($batch) {
206 3
            $this->handleFailedDispatch($batch, $error);
207 24
        });
208 24
    }
209
210
    /**
211
     * Dispatches the given queue in multiple batches.
212
     *
213
     * @param array $queue
214
     * @param int $maxBatchSize
215
     *
216
     * @return void
217
     */
218 1
    private function dispatchQueueInMultipleBatches(array $queue, $maxBatchSize): void
219
    {
220 1
        $numberOfBatchesToDispatch = \count($queue) / $maxBatchSize;
221
222 1
        for ($i = 0; $i < $numberOfBatchesToDispatch; $i++) {
223 1
            $this->dispatchQueueBatch(
224 1
                \array_slice($queue, $i * $maxBatchSize, $maxBatchSize)
225
            );
226
        }
227 1
    }
228
229
    /**
230
     * Handles the batch by resolving the promises and rejecting ones that return Exceptions.
231
     *
232
     * @param array $batch
233
     * @param array $values
234
     */
235 21
    private function handleSuccessfulDispatch(array $batch, array $values): void
236
    {
237 21
        foreach ($batch as $index => $queueItem) {
238 21
            $value = $values[$index];
239 21
            if ($value instanceof \Exception) {
240 4
                $queueItem['reject']($value);
241
            } else {
242 21
                $queueItem['resolve']($value);
243
            }
244
        }
245 21
    }
246
247
    /**
248
     * Handles the failed batch dispatch.
249
     *
250
     * @param array $batch
251
     * @param \Exception $error
252
     */
253 7
    private function handleFailedDispatch(array $batch, \Exception $error)
254
    {
255 7
        foreach ($batch as $index => $queueItem) {
256
            // We don't want to cache individual loads if the entire batch dispatch fails.
257 7
            $this->clear($queueItem['key']);
258 7
            $queueItem['reject']($error);
259
        }
260 7
    }
261
262
    /**
263
     * Validates the batch promise's output.
264
     *
265
     * @param array $values Values from resolved promise.
266
     * @param array $keys Keys which the DataLoaders load was called with
267
     *
268
     * @throws DataLoaderException
269
     */
270 23
    private function validateBatchPromiseOutput($values, $keys): void
271
    {
272 23
        if (! \is_array($values)) {
273 1
            throw new DataLoaderException(
274
                self::class . ' must be constructed with a function which accepts ' .
275
                'an array of keys and returns a Promise which resolves to an array of values ' .
276 1
                \sprintf('not return a Promise: %s.', \gettype($values))
277
            );
278
        }
279
280 22
        if (\count($values) !== \count($keys)) {
281 1
            throw new DataLoaderException(
282
                self::class . ' must be constructed with a function which accepts ' .
283
                'an array of keys and returns a Promise which resolves to an array of values, but ' .
284
                'the function did not return a Promise of an array of the same length as the array of keys.' .
285 1
                \sprintf("\n Keys: %s\n Values: %s\n", \count($keys), \count($values))
286
            );
287
        }
288 21
    }
289
290
    /**
291
     * Validates the batch promise returned from the batch load function.
292
     *
293
     * @param $batchPromise
294
     *
295
     * @throws DataLoaderException
296
     */
297 28
    private function validateBatchPromise($batchPromise): void
298
    {
299 28
        if (! $batchPromise || ! \is_callable([$batchPromise, 'then'])) {
300 4
            throw new DataLoaderException(
301
                self::class . ' must be constructed with a function which accepts ' .
302
                'an array of keys and returns a Promise which resolves to an array of values ' .
303 4
                \sprintf('the function returned %s.', \gettype($batchPromise))
304
            );
305
        }
306 24
    }
307
}
308