Passed
Push — master ( 932af5...f34bcb )
by David
48s queued 10s
created

TdbmFluidColumnGraphqlOptions::addAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
4
namespace TheCodingMachine\FluidSchema;
5
6
7
use function addslashes;
8
use function var_export;
9
10
class TdbmFluidColumnGraphqlOptions
11
{
12
    /**
13
     * @var TdbmFluidColumnOptions
14
     */
15
    private $tdbmFluidColumnOptions;
16
    /**
17
     * @var string
18
     */
19
    private $name;
20
    /**
21
     * @var string
22
     */
23
    private $outputType;
24
25
    public function __construct(TdbmFluidColumnOptions $tdbmFluidColumnOptions)
26
    {
27
        $this->tdbmFluidColumnOptions = $tdbmFluidColumnOptions;
28
    }
29
30
    public function fieldName(string $name): self
31
    {
32
        $this->name = $name;
33
        $this->generateFieldAnnotation();
34
        return $this;
35
    }
36
37
    public function outputType(string $outputType): self
38
    {
39
        $this->outputType = $outputType;
40
        $this->generateFieldAnnotation();
41
        return $this;
42
    }
43
44
    private function generateFieldAnnotation(): void
45
    {
46
        $parameters = array_filter([
47
            'name' => $this->name,
48
            'outputType' => $this->outputType
49
        ]);
50
        if (empty($parameters)) {
51
            $parameters = null;
52
        }
53
        $this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field', $parameters);
54
    }
55
56
    public function logged(bool $mustBeLogged = true): self
57
    {
58
        if ($mustBeLogged) {
59
            $this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Logged');
60
        } else {
61
            $this->removeAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Logged');
62
        }
63
        return $this;
64
    }
65
66
    public function right(string $rightName): self
67
    {
68
        $this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Right', ['name'=>$rightName]);
69
        return $this;
70
    }
71
72
    public function failWith($value): self
73
    {
74
        $this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\FailWith', $value, true, true);
75
        return $this;
76
    }
77
78
    /**
79
     * @param string $annotation
80
     * @param mixed $content
81
     * @param bool $replaceExisting
82
     * @return TdbmFluidColumnGraphqlOptions
83
     */
84
    public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self
85
    {
86
        $this->tdbmFluidColumnOptions->addAnnotation($annotation, $content, $replaceExisting, $explicitNull);
87
        return $this;
88
    }
89
90
    public function removeAnnotation(string $annotation): self
91
    {
92
        $this->tdbmFluidColumnOptions->removeAnnotation($annotation);
93
        return $this;
94
    }
95
96
    public function endGraphql(): TdbmFluidColumnOptions
97
    {
98
        return $this->tdbmFluidColumnOptions;
99
    }
100
101
    public function column(string $name): TdbmFluidColumn
102
    {
103
        return $this->tdbmFluidColumnOptions->column($name);
104
    }
105
}
106