Completed
Pull Request — master (#133)
by Adrien
04:00
created

GlobalIdField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 45
rs 10
c 0
b 0
f 0
ccs 14
cts 16
cp 0.875

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getName() 0 4 1
A getDescription() 0 4 1
A getType() 0 4 1
A resolve() 0 4 2
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 5/10/16 11:23 PM
7
*/
8
9
namespace Youshido\GraphQL\Relay\Field;
10
11
12
use Youshido\GraphQL\Execution\ResolveInfo;
13
use Youshido\GraphQL\Field\AbstractField;
14
use Youshido\GraphQL\Relay\Node;
15
use Youshido\GraphQL\Type\NonNullType;
16
use Youshido\GraphQL\Type\Scalar\IdType;
17
18
class GlobalIdField extends AbstractField
19
{
20
21
    /** @var  string */
22
    protected $typeName;
23
24
    /**
25
     * @param string $typeName
26
     */
27 1
    public function __construct($typeName)
28
    {
29 1
        $this->typeName = $typeName;
30
31
        $config = [
32 1
            'type'    => $this->getType(),
33 1
            'name'    => $this->getName(),
34 1
            'resolve' => [$this, 'resolve']
35 1
        ];
36
37 1
        parent::__construct($config);
38 1
    }
39
40 1
    public function getName()
41
    {
42 1
        return 'id';
43
    }
44
45 1
    public function getDescription()
46
    {
47 1
        return 'The ID of an object';
48
    }
49
50 1
    public function getType()
51
    {
52 1
        return new NonNullType(new IdType());
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function resolve($value, array $args, ResolveInfo $info)
59
    {
60
        return $value ? Node::toGlobalId($this->typeName, $value['id']) : null;
61
    }
62
}
63