Completed
Pull Request — master (#6)
by David
01:52
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 Doctrine\DBAL\Types\Type;
9
use function var_export;
10
11
class TdbmFluidColumnGraphqlOptions
12
{
13
    /**
14
     * @var TdbmFluidColumnOptions
15
     */
16
    private $tdbmFluidColumnOptions;
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
    /**
22
     * @var string
23
     */
24
    private $outputType;
25
    /**
26
     * @var FluidColumn
27
     */
28
    private $fluidColumn;
29
30
    public function __construct(TdbmFluidColumnOptions $tdbmFluidColumnOptions, FluidColumn $fluidColumn)
31
    {
32
        $this->tdbmFluidColumnOptions = $tdbmFluidColumnOptions;
33
        $this->fluidColumn = $fluidColumn;
34
        if (!$this->getComment()->hasAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field')) {
35
            $this->generateFieldAnnotation();
36
        }
37
    }
38
39
    private function getComment(): Comment
40
    {
41
        $comment = $this->fluidColumn->getDbalColumn()->getComment();
42
43
        return new Comment($comment ?? '');
44
    }
45
46
    public function fieldName(string $name): self
47
    {
48
        $this->name = $name;
49
        $this->generateFieldAnnotation();
50
        return $this;
51
    }
52
53
    public function outputType(string $outputType): self
54
    {
55
        $this->outputType = $outputType;
56
        $this->generateFieldAnnotation();
57
        return $this;
58
    }
59
60
    private function generateFieldAnnotation(): void
61
    {
62
        $outputType = null;
63
        if ($this->outputType !== null) {
64
            $outputType = $this->outputType;
65
        } elseif ($this->fluidColumn->getDbalColumn()->getType() === Type::getType(Type::GUID)) {
66
            $outputType = 'ID';
67
        } else {
68
            // If the column is the primary key, let's add an ID type
69
            $pk = $this->tdbmFluidColumnOptions->then()->getDbalTable()->getPrimaryKey();
70
            if ($pk !== null && $pk->getColumns() === [$this->fluidColumn->getDbalColumn()->getName()]) {
71
                $outputType = 'ID';
72
            }
73
        }
74
75
        $parameters = array_filter([
76
            'name' => $this->name,
77
            'outputType' => $outputType
78
        ]);
79
        if (empty($parameters)) {
80
            $parameters = null;
81
        }
82
        $this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field', $parameters);
83
    }
84
85
    public function logged(bool $mustBeLogged = true): self
86
    {
87
        if ($mustBeLogged) {
88
            $this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Logged');
89
        } else {
90
            $this->removeAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Logged');
91
        }
92
        return $this;
93
    }
94
95
    public function right(string $rightName): self
96
    {
97
        $this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Right', ['name'=>$rightName]);
98
        return $this;
99
    }
100
101
    public function failWith($value): self
102
    {
103
        $this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\FailWith', $value, true, true);
104
        return $this;
105
    }
106
107
    /**
108
     * @param string $annotation
109
     * @param mixed $content
110
     * @param bool $replaceExisting
111
     * @return TdbmFluidColumnGraphqlOptions
112
     */
113
    public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self
114
    {
115
        $this->tdbmFluidColumnOptions->addAnnotation($annotation, $content, $replaceExisting, $explicitNull);
116
        return $this;
117
    }
118
119
    public function removeAnnotation(string $annotation): self
120
    {
121
        $this->tdbmFluidColumnOptions->removeAnnotation($annotation);
122
        return $this;
123
    }
124
125
    public function endGraphql(): TdbmFluidColumnOptions
126
    {
127
        return $this->tdbmFluidColumnOptions;
128
    }
129
130
    public function column(string $name): TdbmFluidColumn
131
    {
132
        return $this->tdbmFluidColumnOptions->column($name);
133
    }
134
}
135