Passed
Push — master ( aca3b6...ebbc3d )
by David
52s queued 11s
created

TdbmFluidColumnOptions::comment()   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
    private function getComment(): Comment
112
    {
113
        $comment = $this->fluidColumn->getDbalColumn()->getComment();
114
115
        return new Comment($comment ?? '');
116
    }
117
118
    private function saveComment(Comment $comment): self
119
    {
120
        $this->fluidColumn->getDbalColumn()->setComment($comment->getComment());
121
        return $this;
122
    }
123
124
    /**
125
     * @param string $annotation
126
     * @param mixed $content
127
     * @param bool $replaceExisting
128
     * @return TdbmFluidColumnOptions
129
     */
130
    public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self
131
    {
132
        $comment = $this->getComment()->addAnnotation($annotation, $content, $replaceExisting, $explicitNull);
133
        $this->saveComment($comment);
134
        return $this;
135
    }
136
137
    public function removeAnnotation(string $annotation): self
138
    {
139
        $comment = $this->getComment()->removeAnnotation($annotation);
140
        $this->saveComment($comment);
141
        return $this;
142
    }
143
}
144