Passed
Pull Request — master (#299)
by Christoffer
03:10 queued 37s
created

ResolverRegistry::applyMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Schema\Resolver;
4
5
use Digia\GraphQL\Execution\ResolveInfo;
6
7
class ResolverRegistry implements ResolverRegistryInterface
8
{
9
    /**
10
     * @var ResolverInterface[]
11
     */
12
    protected $resolverMap;
13
14
    /**
15
     * @var ResolverMiddlewareInterface[]
16
     */
17
    protected $middleware;
18
19
    /**
20
     * ResolverRegistry constructor.
21
     * @param ResolverInterface[]           $resolvers
22
     * @param ResolverMiddlewareInterface[] $middleware
23
     */
24
    public function __construct(array $resolvers = [], array $middleware = [])
25
    {
26
        $this->middleware = $middleware;
27
28
        $this->registerResolvers($resolvers);
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function register(string $typeName, ResolverInterface $resolver): void
35
    {
36
        $this->resolverMap[$typeName] = $resolver;
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function getFieldResolver(string $typeName, string $fieldName): ?callable
43
    {
44
        $resolver = $this->getResolver($typeName);
45
46
        if (null === $resolver) {
47
            return null;
48
        }
49
50
        $resolver = $resolver instanceof ResolverCollection
51
            ? $resolver->getResolver($fieldName)
52
            : $resolver;
53
54
        $resolveCallback = $resolver instanceof ResolverInterface
55
            ? $resolver->getResolveCallback()
56
            : $resolver;
57
58
        if (!empty($this->middleware)) {
59
            return $this->applyMiddleware($resolveCallback, \array_reverse($this->middleware));
0 ignored issues
show
Bug introduced by
It seems like $resolveCallback can also be of type null; however, parameter $resolveCallback of Digia\GraphQL\Schema\Res...stry::applyMiddleware() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            return $this->applyMiddleware(/** @scrutinizer ignore-type */ $resolveCallback, \array_reverse($this->middleware));
Loading history...
60
        }
61
62
        return $resolveCallback;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function getTypeResolver(string $typeName): ?callable
69
    {
70
        $resolver = $this->getResolver($typeName);
71
72
        if ($resolver instanceof ResolverInterface) {
73
            return $resolver->getTypeResolver();
74
        }
75
76
        return null;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function getResolver(string $typeName): ?ResolverInterface
83
    {
84
        return $this->resolverMap[$typeName] ?? null;
85
    }
86
87
    /**
88
     * @param array $resolvers
89
     */
90
    protected function registerResolvers(array $resolvers): void
91
    {
92
        foreach ($resolvers as $typeName => $resolver) {
93
            if (\is_array($resolver)) {
94
                $resolver = new ResolverCollection($resolver);
95
            }
96
97
            $this->register($typeName, $resolver);
98
        }
99
    }
100
101
    /**
102
     * @param callable $resolveCallback
103
     * @param array    $middleware
104
     * @return callable
105
     */
106
    protected function applyMiddleware(callable $resolveCallback, array $middleware): callable
107
    {
108
        return \array_reduce(
109
            $middleware,
110
            function (callable $resolveCallback, ResolverMiddlewareInterface $middleware) {
111
                return function ($rootValue, array $args, $contextValues = null, ?ResolveInfo $info = null) use (
112
                    $resolveCallback,
113
                    $middleware
114
                ) {
115
                    return $middleware->resolve($resolveCallback, $rootValue, $args, $contextValues, $info);
116
                };
117
            },
118
            $resolveCallback
119
        );
120
    }
121
}
122