Passed
Push — master ( eb658c...82f8ff )
by David
57s
created

TdbmFluidColumnOptions::saveComment()   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 1
1
<?php
2
3
4
namespace TheCodingMachine\FluidSchema;
5
6
7
class TdbmFluidColumnOptions
8
{
9
    /**
10
     * @var TdbmFluidTable
11
     */
12
    private $tdbmFluidTable;
13
    /**
14
     * @var FluidColumn
15
     */
16
    private $fluidColumn;
17
    /**
18
     * @var FluidColumnOptions
19
     */
20
    private $fluidColumnOptions;
21
22
    public function __construct(TdbmFluidTable $tdbmFluidTable, FluidColumn $fluidColumn, FluidColumnOptions $fluidColumnOptions)
23
    {
24
        $this->tdbmFluidTable = $tdbmFluidTable;
25
        $this->fluidColumn = $fluidColumn;
26
        $this->fluidColumnOptions = $fluidColumnOptions;
27
    }
28
29
    /**
30
     * Makes the column not nullable.
31
     * @return FluidColumnOptions
32
     */
33
    public function notNull(): self
34
    {
35
        $this->fluidColumnOptions->notNull();
36
        return $this;
37
    }
38
39
    /**
40
     * Makes the column nullable.
41
     * @return self
42
     */
43
    public function null(): self
44
    {
45
        $this->fluidColumnOptions->null();
46
        return $this;
47
    }
48
49
    /**
50
     * Automatically add a unique constraint for the column.
51
     *
52
     * @return self
53
     */
54
    public function unique(): self
55
    {
56
        $this->fluidColumnOptions->unique();
57
        return $this;
58
    }
59
60
    /**
61
     * Automatically add an index for the column.
62
     *
63
     * @return self
64
     */
65
    public function index(): self
66
    {
67
        $this->fluidColumnOptions->index();
68
        return $this;
69
    }
70
    public function comment(string $comment): self
71
    {
72
        $this->fluidColumnOptions->comment($comment);
73
        return $this;
74
    }
75
76
    public function autoIncrement(): self
77
    {
78
        $this->fluidColumnOptions->autoIncrement();
79
        $this->addAnnotation('Autoincrement');
80
        return $this;
81
    }
82
83
    public function primaryKey(?string $indexName = null): self
84
    {
85
        $this->fluidColumnOptions->primaryKey($indexName);
86
        return $this;
87
    }
88
89
    public function default($defaultValue): self
90
    {
91
        $this->fluidColumnOptions->default($defaultValue);
92
        return $this;
93
    }
94
95
    public function then(): TdbmFluidTable
96
    {
97
        return $this->tdbmFluidTable;
98
    }
99
100
    public function column(string $name): TdbmFluidColumn
101
    {
102
        return $this->tdbmFluidTable->column($name);
103
    }
104
105
    public function graphqlField(): TdbmFluidColumnGraphqlOptions
106
    {
107
        $this->tdbmFluidTable->graphqlType();
108
        return new TdbmFluidColumnGraphqlOptions($this, $this->fluidColumn);
109
    }
110
111
    public function protectedGetter(): self
112
    {
113
        $this->addAnnotation('TheCodingMachine\\TDBM\\Utils\\Annotation\\ProtectedGetter');
114
        return $this;
115
    }
116
117
    public function protectedSetter(): self
118
    {
119
        $this->addAnnotation('TheCodingMachine\\TDBM\\Utils\\Annotation\\ProtectedSetter');
120
        return $this;
121
    }
122
123
    public function protectedOneToMany(): self
124
    {
125
        $this->addAnnotation('TheCodingMachine\\TDBM\\Utils\\Annotation\\ProtectedOneToMany');
126
        return $this;
127
    }
128
129
    private function getComment(): Comment
130
    {
131
        $comment = $this->fluidColumn->getDbalColumn()->getComment();
132
133
        return new Comment($comment ?? '');
134
    }
135
136
    private function saveComment(Comment $comment): self
137
    {
138
        $this->fluidColumn->getDbalColumn()->setComment($comment->getComment());
139
        return $this;
140
    }
141
142
    /**
143
     * @param string $annotation
144
     * @param mixed $content
145
     * @param bool $replaceExisting
146
     * @return TdbmFluidColumnOptions
147
     */
148
    public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self
149
    {
150
        $comment = $this->getComment()->addAnnotation($annotation, $content, $replaceExisting, $explicitNull);
151
        $this->saveComment($comment);
152
        return $this;
153
    }
154
155
    public function removeAnnotation(string $annotation): self
156
    {
157
        $comment = $this->getComment()->removeAnnotation($annotation);
158
        $this->saveComment($comment);
159
        return $this;
160
    }
161
}
162