Passed
Push — improve-filter-message ( b945de...00229f )
by Han Hui
04:37
created

ItemResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 31
c 1
b 0
f 0
dl 0
loc 49
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 29 4
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 ApiPlatform\Core\Util\CloneTrait;
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 CloneTrait;
37
    use ClassInfoTrait;
38
    use FieldsToAttributesTrait;
39
    use ResourceAccessCheckerTrait;
0 ignored issues
show
introduced by
The trait ApiPlatform\Core\GraphQl...ourceAccessCheckerTrait requires some properties which are not provided by ApiPlatform\Core\GraphQl\Resolver\ItemResolver: $fieldNodes, $path
Loading history...
40
41
    private $iriConverter;
42
    private $resourceAccessChecker;
43
    private $normalizer;
44
    private $resourceMetadataFactory;
45
46
    public function __construct(IriConverterInterface $iriConverter, NormalizerInterface $normalizer, ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourceAccessCheckerInterface $resourceAccessChecker = null)
47
    {
48
        $this->iriConverter = $iriConverter;
49
        $this->normalizer = $normalizer;
50
        $this->resourceMetadataFactory = $resourceMetadataFactory;
51
        $this->resourceAccessChecker = $resourceAccessChecker;
52
    }
53
54
    public function __invoke($source, $args, $context, ResolveInfo $info)
55
    {
56
        // Data already fetched and normalized (field or nested resource)
57
        if (isset($source[$info->fieldName])) {
58
            return $source[$info->fieldName];
59
        }
60
61
        if (!isset($args['id'])) {
62
            return null;
63
        }
64
65
        $baseNormalizationContext = ['attributes' => $this->fieldsToAttributes($info)];
66
        try {
67
            $item = $this->iriConverter->getItemFromIri($args['id'], $baseNormalizationContext);
68
        } catch (ItemNotFoundException $e) {
69
            return null;
70
        }
71
72
        $resourceClass = $this->getObjectClass($item);
73
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
74
        $this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, [
75
            'object' => $item,
76
            'previous_object' => $this->clone($item),
77
        ], 'query');
78
79
        $normalizationContext = $resourceMetadata->getGraphqlAttribute('query', 'normalization_context', [], true);
80
        $normalizationContext['resource_class'] = $resourceClass;
81
82
        return $this->normalizer->normalize($item, ItemNormalizer::FORMAT, $normalizationContext + $baseNormalizationContext);
83
    }
84
}
85