Next::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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