Passed
Push — master ( 8d625d...e4f45d )
by Kévin
03:56 queued 11s
created

ItemResolverFactory::__invoke()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 35
rs 9.2888
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\GraphQl\Resolver\Factory;
15
16
use ApiPlatform\Core\GraphQl\Resolver\QueryItemResolverInterface;
17
use ApiPlatform\Core\GraphQl\Resolver\Stage\DenyAccessStageInterface;
18
use ApiPlatform\Core\GraphQl\Resolver\Stage\ReadStageInterface;
19
use ApiPlatform\Core\GraphQl\Resolver\Stage\SerializeStageInterface;
20
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
21
use ApiPlatform\Core\Util\ClassInfoTrait;
22
use ApiPlatform\Core\Util\CloneTrait;
23
use GraphQL\Error\Error;
24
use GraphQL\Type\Definition\ResolveInfo;
25
use Psr\Container\ContainerInterface;
26
27
/**
28
 * Creates a function retrieving an item to resolve a GraphQL query.
29
 *
30
 * @experimental
31
 *
32
 * @author Alan Poulain <[email protected]>
33
 * @author Kévin Dunglas <[email protected]>
34
 */
35
final class ItemResolverFactory implements ResolverFactoryInterface
36
{
37
    use CloneTrait;
38
    use ClassInfoTrait;
39
40
    private $readStage;
41
    private $denyAccessStage;
42
    private $serializeStage;
43
    private $queryResolverLocator;
44
    private $resourceMetadataFactory;
45
46
    public function __construct(ReadStageInterface $readStage, DenyAccessStageInterface $denyAccessStage, SerializeStageInterface $serializeStage, ContainerInterface $queryResolverLocator, ResourceMetadataFactoryInterface $resourceMetadataFactory)
47
    {
48
        $this->readStage = $readStage;
49
        $this->denyAccessStage = $denyAccessStage;
50
        $this->serializeStage = $serializeStage;
51
        $this->queryResolverLocator = $queryResolverLocator;
52
        $this->resourceMetadataFactory = $resourceMetadataFactory;
53
    }
54
55
    public function __invoke(?string $resourceClass = null, ?string $rootClass = null, ?string $operationName = null): callable
56
    {
57
        return function (?array $source, array $args, $context, ResolveInfo $info) use ($resourceClass, $rootClass, $operationName) {
58
            // Data already fetched and normalized (field or nested resource)
59
            if (isset($source[$info->fieldName])) {
60
                return $source[$info->fieldName];
61
            }
62
63
            $operationName = $operationName ?? 'query';
64
            $resolverContext = ['source' => $source, 'args' => $args, 'info' => $info, 'is_collection' => false, 'is_mutation' => false];
65
66
            $item = ($this->readStage)($resourceClass, $rootClass, $operationName, $resolverContext);
67
            if (null !== $item && !\is_object($item)) {
68
                throw new \LogicException('Item from read stage should be a nullable object.');
69
            }
70
71
            $resourceClass = $this->getResourceClass($item, $resourceClass, $info);
72
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
73
74
            $queryResolverId = $resourceMetadata->getGraphqlAttribute($operationName, 'item_query');
75
            if (null !== $queryResolverId) {
76
                /** @var QueryItemResolverInterface $queryResolver */
77
                $queryResolver = $this->queryResolverLocator->get($queryResolverId);
78
                $item = $queryResolver($item, ['source' => $source, 'args' => $args, 'info' => $info]);
79
                $resourceClass = $this->getResourceClass($item, $resourceClass, $info, sprintf('Custom query resolver "%s"', $queryResolverId).' has to return an item of class %s but returned an item of class %s.');
80
            }
81
82
            ($this->denyAccessStage)($resourceClass, $operationName, $resolverContext + [
83
                'extra_variables' => [
84
                    'object' => $item,
85
                    'previous_object' => $this->clone($item),
86
                ],
87
            ]);
88
89
            return ($this->serializeStage)($item, $resourceClass, $operationName, $resolverContext);
90
        };
91
    }
92
93
    /**
94
     * @param object|null $item
95
     *
96
     * @throws Error
97
     */
98
    private function getResourceClass($item, ?string $resourceClass, ResolveInfo $info, string $errorMessage = 'Resolver only handles items of class %s but retrieved item is of class %s.'): string
99
    {
100
        if (null === $item) {
101
            if (null === $resourceClass) {
102
                throw Error::createLocatedError('Resource class cannot be determined.', $info->fieldNodes, $info->path);
103
            }
104
105
            return $resourceClass;
106
        }
107
108
        $itemClass = $this->getObjectClass($item);
109
110
        if (null === $resourceClass) {
111
            return $itemClass;
112
        }
113
114
        if ($resourceClass !== $itemClass) {
115
            throw Error::createLocatedError(sprintf($errorMessage, (new \ReflectionClass($resourceClass))->getShortName(), (new \ReflectionClass($itemClass))->getShortName()), $info->fieldNodes, $info->path);
116
        }
117
118
        return $resourceClass;
119
    }
120
}
121