Next::resolve()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 9
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\ArgumentResolver\Middleware;
6
7
use Andi\GraphQL\ArgumentResolver\ArgumentResolverInterface;
8
use Andi\GraphQL\Exception\NextHandlerIsEmptyException;
9
use SplPriorityQueue;
10
11
final class Next implements ArgumentResolverInterface
12
{
13
    private SplPriorityQueue $queue;
14
15 3
    public function __construct(
16
        SplPriorityQueue $queue,
17
        private readonly ArgumentResolverInterface $fallbackResolver,
18
    ) {
19 3
        $this->queue = clone $queue;
20
    }
21
22 2
    public function resolve(mixed $argument): array
23
    {
24
        /** @psalm-suppress RedundantPropertyInitializationCheck */
25 2
        if (! isset($this->queue)) {
26 1
            throw new NextHandlerIsEmptyException('Cannot invoke pipeline resolver more than once');
27
        }
28
29 2
        if ($this->queue->isEmpty()) {
30 1
            unset($this->queue);
31
32 1
            return $this->fallbackResolver->resolve($argument);
33
        }
34
35
        /** @var MiddlewareInterface $middleware */
36 1
        $middleware = $this->queue->extract();
37
38 1
        $next = clone $this;
39 1
        unset($this->queue);
40
41 1
        return $middleware->process($argument, $next);
42
    }
43
}
44