Passed
Pull Request — 2.3 (#2195)
by Kévin
03:42 queued 01:04
created

ItemResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 33
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 25 4
A replaceIdKey() 0 16 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 GraphQL\Type\Definition\ResolveInfo;
23
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
24
25
/**
26
 * Creates a function retrieving an item to resolve a GraphQL query.
27
 *
28
 * @experimental
29
 *
30
 * @author Alan Poulain <[email protected]>
31
 * @author Kévin Dunglas <[email protected]>
32
 */
33
final class ItemResolver
34
{
35
    use ClassInfoTrait;
36
    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...
37
38
    private $iriConverter;
39
    private $resourceAccessChecker;
40
    private $normalizer;
41
    private $resourceMetadataFactory;
42
43
    public function __construct(IriConverterInterface $iriConverter, NormalizerInterface $normalizer, ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourceAccessCheckerInterface $resourceAccessChecker = null)
44
    {
45
        $this->iriConverter = $iriConverter;
46
        $this->normalizer = $normalizer;
47
        $this->resourceMetadataFactory = $resourceMetadataFactory;
48
        $this->resourceAccessChecker = $resourceAccessChecker;
49
    }
50
51
    public function __invoke($source, $args, $context, ResolveInfo $info)
52
    {
53
        // Data already fetched and normalized (field or nested resource)
54
        if (isset($source[$info->fieldName])) {
55
            return $source[$info->fieldName];
56
        }
57
58
        if (!isset($args['id'])) {
59
            return null;
60
        }
61
62
        $baseNormalizationContext = ['attributes' => $this->replaceIdKey($info->getFieldSelection(PHP_INT_MAX))];
63
        try {
64
            $item = $this->iriConverter->getItemFromIri($args['id'], $baseNormalizationContext);
65
        } catch (ItemNotFoundException $e) {
66
            return null;
67
        }
68
69
        $resourceClass = $this->getObjectClass($item);
70
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
71
        $this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $item, 'query');
72
73
        $normalizationContext = $resourceMetadata->getGraphqlAttribute('query', 'normalization_context', [], true);
74
75
        return $this->normalizer->normalize($item, ItemNormalizer::FORMAT, $normalizationContext + $baseNormalizationContext);
76
    }
77
78
    /**
79
     * Recursively replaces the key "_id" (the raw id i GraphQL) by "id" (the name of the property expected by the Serializer).
80
     */
81
    private function replaceIdKey(array $array): array
82
    {
83
        foreach ($array as $key => $value) {
84
            if ('_id' === $key) {
85
                $array['id'] = true;
86
                unset($array['key']);
87
88
                continue;
89
            }
90
91
            if (\is_array($array[$key])) {
92
                $array[$key] = $this->replaceIdKey($array[$key]);
93
            }
94
        }
95
96
        return $array;
97
    }
98
}
99