Passed
Push — master ( c85183...4934b6 )
by Christoffer
03:11
created

ResolverMap::getTypeResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Schema\Resolver;
4
5
class ResolverMap implements ResolverCollectionInterface
6
{
7
    protected const TYPE_RESOLVER_KEY = '__resolveType';
8
9
    /**
10
     * @var callable[]
11
     */
12
    protected $resolvers;
13
14
    /**
15
     * ResolverCollection constructor.
16
     * @param callable[] $resolvers
17
     */
18
    public function __construct(array $resolvers)
19
    {
20
        $this->registerResolvers($resolvers);
21
    }
22
23
    /**
24
     * @param string   $fieldName
25
     * @param callable $resolver
26
     */
27
    public function addResolver(string $fieldName, callable $resolver)
28
    {
29
        $this->resolvers[$fieldName] = $resolver;
30
    }
31
32
    /**
33
     * @param string $fieldName
34
     * @return callable|null
35
     */
36
    public function getResolver(string $fieldName): ?callable
37
    {
38
        return $this->resolvers[$fieldName] ?? null;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function getResolveCallback(): ?callable
45
    {
46
        return function (string $fieldName) {
47
            $resolver = $this->getResolver($fieldName);
48
49
            return $resolver instanceof ResolverInterface
50
                ? $resolver->getResolveCallback()
51
                : $resolver;
52
        };
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function getTypeResolver(): ?callable
59
    {
60
        return $this->resolvers[static::TYPE_RESOLVER_KEY] ?? null;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function getMiddleware(): ?array
67
    {
68
        return null;
69
    }
70
71
    /**
72
     * @param array $resolvers
73
     */
74
    protected function registerResolvers(array $resolvers): void
75
    {
76
        foreach ($resolvers as $typeName => $resolver) {
77
            if (\is_array($resolver)) {
78
                $resolver = new ResolverMap($resolver);
79
            }
80
81
            $this->addResolver($typeName, $resolver);
82
        }
83
    }
84
}
85