Completed
Push — 1.x ( c74803...3a2761 )
by David
02:58
created

Field::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\Tdbm\GraphQL;
5
6
use TheCodingMachine\Tdbm\GraphQL\Registry\Registry;
7
use Youshido\GraphQL\Execution\ResolveInfo;
8
use Youshido\GraphQL\Field\AbstractField;
9
use Youshido\GraphQL\Type\AbstractType;
10
use Youshido\GraphQL\Type\Object\AbstractObjectType;
11
use Youshido\GraphQL\Type\TypeInterface;
12
13
class Field extends AbstractField
14
{
15
    private $hide = false;
16
    private $right;
17
18
    /**
19
     * @var Registry
20
     */
21
    private $registry;
22
23
    public function __construct(string $name, TypeInterface $type, Registry $registry, array $additionalConfig = [])
24
    {
25
        $this->registry = $registry;
26
27
        $config = [
28
            'name' => $name,
29
            'type' => $type
30
        ];
31
32
        if (!isset($additionalConfig['resolve'])) {
33
            $config['resolve'] = function ($source, array $args, ResolveInfo $info) {
34
                $getter = 'get'.$info->getField()->getName();
35
                return $source->$getter();
36
            };
37
        }
38
39
        $config += $additionalConfig;
40
        parent::__construct($config);
41
    }
42
    
43
    /**
44
     * @return AbstractObjectType|AbstractType
45
     */
46
    public function getType()
47
    {
48
        return $this->config->getType();
49
    }
50
51
    /**
52
     * @return bool
53
     */
54
    public function isHidden(): bool
55
    {
56
        $hide = $this->hide;
57
        if ($this->right !== null) {
58
            $hide |= !$this->registry->getAuthorizationService()->isAllowed($this->right);
59
        }
60
        return $hide;
61
    }
62
63
    /**
64
     * Hides this field.
65
     */
66
    public function hide()
67
    {
68
        $this->hide = true;
69
    }
70
71
    /**
72
     * Show this field if it was previously hidden.
73
     */
74
    public function show()
75
    {
76
        $this->hide = false;
77
    }
78
79
    public function requiresRight(string $right)
80
    {
81
        if ($this->registry->getAuthorizationService() === null) {
82
            throw new GraphQLException('You did not configure an authorization in the TDBM-GraphQL registry.');
83
        }
84
        $this->right = $right;
85
    }
86
}
87