Passed
Pull Request — 2.1 (#1729)
by Antoine
04:12 queued 01:10
created

ItemResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C __invoke() 0 32 7
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;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use ApiPlatform\Core\Exception\ItemNotFoundException;
18
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
19
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
20
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
21
use ApiPlatform\Core\Util\ClassInfoTrait;
22
use GraphQL\Error\Error;
23
use GraphQL\Type\Definition\ResolveInfo;
24
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25
26
/**
27
 * Creates a function retrieving an item to resolve a GraphQL query.
28
 *
29
 * @experimental
30
 *
31
 * @author Alan Poulain <[email protected]>
32
 * @author Kévin Dunglas <[email protected]>
33
 */
34
final class ItemResolver
35
{
36
    use ClassInfoTrait;
37
    use ResourceAccessCheckerTrait;
38
39
    private $iriConverter;
40
    private $resourceAccessChecker;
41
    private $normalizer;
42
    private $resourceMetadataFactory;
43
44
    public function __construct(IriConverterInterface $iriConverter, NormalizerInterface $normalizer, ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourceAccessCheckerInterface $resourceAccessChecker = null)
45
    {
46
        $this->iriConverter = $iriConverter;
47
        $this->normalizer = $normalizer;
48
        $this->resourceMetadataFactory = $resourceMetadataFactory;
49
        $this->resourceAccessChecker = $resourceAccessChecker;
50
    }
51
52
    public function __invoke($source, $args, $context, ResolveInfo $info)
53
    {
54
        // Data already fetched and normalized (field or nested resource)
55
        if (isset($source[$info->fieldName])) {
56
            return $source[$info->fieldName];
57
        }
58
59
        if (!isset($args['id'])) {
60
            return null;
61
        }
62
63
        $baseNormalizationContext = ['attributes' => $info->getFieldSelection(PHP_INT_MAX)];
64
        try {
65
            $item = $this->iriConverter->getItemFromIri($args['id'], $baseNormalizationContext);
66
        } catch (ItemNotFoundException $e) {
67
            return null;
68
        }
69
70
        $resourceClass = $this->getObjectClass($item);
71
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
72
        $this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $item, 'query');
73
74
        if (null !== $this->resourceAccessChecker) {
75
            $isGranted = $resourceMetadata->getGraphqlAttribute('query', 'access_control', null, true);
76
            if (null !== $isGranted && !$this->resourceAccessChecker->isGranted($resourceClass, $isGranted, ['object' => $item])) {
77
                throw Error::createLocatedError('Access Denied.', $info->fieldNodes, $info->path);
78
            }
79
        }
80
81
        $normalizationContext = $resourceMetadata->getGraphqlAttribute('query', 'normalization_context', [], true);
82
83
        return $this->normalizer->normalize($item, ItemNormalizer::FORMAT, $normalizationContext + $baseNormalizationContext);
84
    }
85
}
86