Completed
Pull Request — 2.1 (#1607)
by Grégoire
03:23
created

ItemResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 11.54 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 8
dl 6
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C __invoke() 6 33 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        // TODO: initialize the EagerLoading extension
64
        try {
65
            $item = $this->iriConverter->getItemFromIri($args['id']);
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);
73
74 View Code Duplication
        if (null !== $this->resourceAccessChecker) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 + ['attributes' => $info->getFieldSelection(PHP_INT_MAX)]);
84
    }
85
}
86