Passed
Pull Request — master (#299)
by Christoffer
03:27 queued 01:05
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[]|null
16
     */
17
    protected $middleware;
18
19
    /**
20
     * ResolverRegistry constructor.
21
     * @param ResolverInterface[]                $resolvers
22
     * @param ResolverMiddlewareInterface[]|null $middleware
23
     */
24
    public function __construct(array $resolvers = [], ?array $middleware = null)
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 (null === $resolveCallback) {
59
            return null;
60
        }
61
62
        if (null !== $this->middleware) {
63
            $middleware = \array_reverse($this->middleware);
64
            return $this->applyMiddleware($resolveCallback, $middleware);
65
        }
66
67
        return $resolveCallback;
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function getTypeResolver(string $typeName): ?callable
74
    {
75
        $resolver = $this->getResolver($typeName);
76
77
        if ($resolver instanceof ResolverInterface) {
78
            return $resolver->getTypeResolver();
79
        }
80
81
        return null;
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function getResolver(string $typeName): ?ResolverInterface
88
    {
89
        return $this->resolverMap[$typeName] ?? null;
90
    }
91
92
    /**
93
     * @param array $resolvers
94
     */
95
    protected function registerResolvers(array $resolvers): void
96
    {
97
        foreach ($resolvers as $typeName => $resolver) {
98
            if (\is_array($resolver)) {
99
                $resolver = new ResolverCollection($resolver);
100
            }
101
102
            $this->register($typeName, $resolver);
103
        }
104
    }
105
106
    /**
107
     * @param callable $resolveCallback
108
     * @param array    $middleware
109
     * @return callable
110
     */
111
    protected function applyMiddleware(callable $resolveCallback, array $middleware): callable
112
    {
113
        return \array_reduce(
114
            $middleware,
115
            function (callable $resolveCallback, ResolverMiddlewareInterface $middleware) {
116
                return function ($rootValue, array $arguments, $context = null, ?ResolveInfo $info = null) use (
117
                    $resolveCallback,
118
                    $middleware
119
                ) {
120
                    return $middleware->resolve($resolveCallback, $rootValue, $arguments, $context, $info);
121
                };
122
            },
123
            $resolveCallback
124
        );
125
    }
126
}
127