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