Passed
Push — master ( ca13db...21415a )
by Adrien
04:36
created

AbstractAttribute::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
eloc 2
ccs 3
cts 3
cp 1
cc 2
nc 2
nop 4
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Attribute;
6
7
use GraphQL\Type\Definition\Type;
8
9
/**
10
 * Abstract attribute with common logic for Argument and Field.
11
 */
12
abstract class AbstractAttribute implements ApiAttribute
13
{
14
    protected const NO_VALUE_PASSED = '_hacky_no_value_passed_marker_';
15
16
    /**
17
     * Instance of the GraphQL type.
18
     */
19
    private ?Type $typeInstance = null;
20
21
    private mixed $defaultValue;
22
23
    private bool $hasDefaultValue = false;
24
25
    /**
26
     * @param null|string $type FQCN of PHP class implementing the GraphQL type
27
     */
28 17
    public function __construct(
29
        private ?string $name,
30
        private readonly ?string $type,
31
        private ?string $description,
32
        mixed $defaultValue,
33
    ) {
34 17
        if ($defaultValue !== self::NO_VALUE_PASSED) {
35 1
            $this->setDefaultValue($defaultValue);
36
        }
37
    }
38
39 12
    public function toArray(): array
40
    {
41 12
        $data = [
42 12
            'name' => $this->getName(),
43 12
            'type' => $this->getTypeInstance(),
44 12
            'description' => $this->getDescription(),
45
        ];
46
47 12
        if ($this->hasDefaultValue()) {
48 10
            $data['defaultValue'] = $this->getDefaultValue();
49
        }
50
51 12
        return $data;
52
    }
53
54 13
    public function getName(): ?string
55
    {
56 13
        return $this->name;
57
    }
58
59 16
    public function setName(string $name): void
60
    {
61 16
        $this->name = $name;
62
    }
63
64 16
    public function getType(): ?string
65
    {
66 16
        return $this->type;
67
    }
68
69 16
    public function getDescription(): ?string
70
    {
71 16
        return $this->description;
72
    }
73
74 16
    public function setDescription(?string $description): void
75
    {
76 16
        $this->description = $description;
77
    }
78
79 16
    public function hasDefaultValue(): bool
80
    {
81 16
        return $this->hasDefaultValue;
82
    }
83
84 10
    public function getDefaultValue(): mixed
85
    {
86 10
        return $this->defaultValue;
87
    }
88
89 10
    public function setDefaultValue(mixed $defaultValue): void
90
    {
91 10
        $this->defaultValue = $defaultValue;
92 10
        $this->hasDefaultValue = true;
93
    }
94
95 16
    public function getTypeInstance(): ?Type
96
    {
97 16
        return $this->typeInstance;
98
    }
99
100 16
    public function setTypeInstance(?Type $typeInstance): void
101
    {
102 16
        $this->typeInstance = $typeInstance;
103
    }
104
}
105