Passed
Push — master ( ce47d9...c1ea42 )
by David
38s
created

TdbmFluidTable::customBeanName()   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
use function addslashes;
7
use Doctrine\DBAL\Schema\Table;
8
use function in_array;
9
10
class TdbmFluidTable
11
{
12
    /**
13
     * @var FluidSchema|TdbmFluidSchema
14
     */
15
    private $schema;
16
    /**
17
     * @var FluidTable
18
     */
19
    private $fluidTable;
20
    /**
21
     * @var NamingStrategyInterface
22
     */
23
    private $namingStrategy;
24
25
    /**
26
     * @var array<string, TdbmFluidColumn>
27
     */
28
    private $tdbmFluidColumns = [];
29
30
    public function __construct(TdbmFluidSchema $schema, FluidTable $fluidTable, NamingStrategyInterface $namingStrategy)
31
    {
32
        $this->schema = $schema;
33
        $this->fluidTable = $fluidTable;
34
        $this->namingStrategy = $namingStrategy;
35
    }
36
37
    public function column(string $name): TdbmFluidColumn
38
    {
39
        if (!isset($this->tdbmFluidColumns[$name])) {
40
            $this->tdbmFluidColumns[$name] = new TdbmFluidColumn($this, $this->fluidTable->column($name), $this->namingStrategy);
41
        }
42
        return $this->tdbmFluidColumns[$name];
43
    }
44
45
    public function index(array $columnNames): TdbmFluidTable
46
    {
47
        $this->fluidTable->index($columnNames);
48
        return $this;
49
    }
50
51
    public function unique(array $columnNames): TdbmFluidTable
52
    {
53
        $this->fluidTable->unique($columnNames);
54
        return $this;
55
    }
56
57
    public function primaryKey(array $columnNames, ?string $indexName = null): TdbmFluidTable
58
    {
59
        $this->fluidTable->primaryKey($columnNames, $indexName);
60
        return $this;
61
    }
62
63
    public function id(): TdbmFluidTable
64
    {
65
        $this->fluidTable->id();
66
        return $this;
67
    }
68
69
    public function uuid(string $version = 'v4'): TdbmFluidTable
70
    {
71
        if ($version !== 'v1' && $version !== 'v4') {
72
            throw new FluidSchemaException('UUID version must be one of "v1" or "v4"');
73
        }
74
        $this->fluidTable->uuid();
75
76
        $this->column('uuid')->guid()->addAnnotation('UUID', '("'.$version.'")');
77
        return $this;
78
    }
79
80
    public function timestamps(): TdbmFluidTable
81
    {
82
        $this->fluidTable->timestamps();
83
        return $this;
84
    }
85
86
    /**
87
     * @throws FluidSchemaException
88
     */
89
    public function extends(string $tableName): TdbmFluidTable
90
    {
91
        $this->fluidTable->extends($tableName);
92
        return $this;
93
    }
94
95
    /**
96
     * Adds a "Bean" annotation to the table.
97
     */
98
    public function customBeanName(string $beanName): TdbmFluidTable
99
    {
100
        $this->addAnnotation('Bean', '(name="'.addslashes($beanName).'")');
101
        return $this;
102
    }
103
104
    /**
105
     * Adds a "Type" annotation.
106
     */
107
    public function graphqlType(): TdbmFluidTable
108
    {
109
        $this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Type');
110
        return $this;
111
    }
112
113
    private function getComment(): Comment
114
    {
115
        $options = $this->fluidTable->getDbalTable()->getOptions();
116
        $comment = $options['comment'] ?? '';
117
118
        return new Comment($comment);
119
    }
120
121
    private function saveComment(Comment $comment): self
122
    {
123
        $this->fluidTable->getDbalTable()->addOption('comment', $comment->getComment());
124
        return $this;
125
    }
126
127
    public function addAnnotation(string $annotation, string $content = '', bool $replaceExisting = true): self
128
    {
129
        $comment = $this->getComment()->addAnnotation($annotation, $content, $replaceExisting);
130
        $this->saveComment($comment);
131
        return $this;
132
    }
133
134
    public function removeAnnotation(string $annotation): self
135
    {
136
        $comment = $this->getComment()->removeAnnotation($annotation);
137
        $this->saveComment($comment);
138
        return $this;
139
    }
140
141
    public function getDbalTable(): Table
142
    {
143
        return $this->fluidTable->getDbalTable();
144
    }
145
}
146