Completed
Pull Request — master (#1569)
by Kévin
03:14
created

ResourceFieldResolver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 17 9
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 GraphQL\Type\Definition\ResolveInfo;
18
19
/**
20
 * A field resolver that resolves IDs to IRIs and allow to access to the raw ID using the "#id" field.
21
 *
22
 * @experimental
23
 *
24
 * @author Kévin Dunglas <[email protected]>
25
 */
26
final class ResourceFieldResolver
27
{
28
    private $iriConverter;
29
30
    public function __construct(IriConverterInterface $iriConverter)
31
    {
32
        $this->iriConverter = $iriConverter;
33
    }
34
35
    public function __invoke($source, $args, $context, ResolveInfo $info)
36
    {
37
        $property = null;
38
        if ('id' === $info->fieldName && isset($source['#item'])) {
39
            return $this->iriConverter->getIriFromItem(unserialize($source['#item']));
40
        }
41
42
        if ('_id' === $info->fieldName && isset($source['id'])) {
43
            $property = $source['id'];
44
        } elseif (\is_array($source) && isset($source[$info->fieldName])) {
45
            $property = $source[$info->fieldName];
46
        } elseif (isset($source->{$info->fieldName})) {
47
            $property = $source->{$info->fieldName};
48
        }
49
50
        return $property instanceof \Closure ? $property($source, $args, $context) : $property;
51
    }
52
}
53