Failed Conditions
Pull Request — master (#28)
by Dallas
06:04
created

src/Schema/Blueprint.php (6 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\Postgres\Schema;
6
7
use Doctrine\DBAL\DriverManager;
8
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
9
use Illuminate\Database\Schema\ColumnDefinition;
10
use Illuminate\Support\Facades\Schema;
11
use Illuminate\Support\Fluent;
12
use Umbrellio\Postgres\Schema\Builders\Constraints\Check\CheckBuilder;
13
use Umbrellio\Postgres\Schema\Builders\Constraints\Exclude\ExcludeBuilder;
14
use Umbrellio\Postgres\Schema\Builders\Indexes\Unique\UniqueBuilder;
15
use Umbrellio\Postgres\Schema\Builders\Routines\CreateFunctionBuilder;
16
use Umbrellio\Postgres\Schema\Builders\Routines\CreateProcedureBuilder;
17
use Umbrellio\Postgres\Schema\Builders\Routines\CreateTriggerBuilder;
18
use Umbrellio\Postgres\Schema\Builders\Routines\DropFunctionBuilder;
19
use Umbrellio\Postgres\Schema\Builders\Routines\DropProcedureBuilder;
20
use Umbrellio\Postgres\Schema\Builders\Routines\DropTriggerBuilder;
21
use Umbrellio\Postgres\Schema\Definitions\Indexes\CheckDefinition;
22
use Umbrellio\Postgres\Schema\Definitions\Indexes\ExcludeDefinition;
23
use Umbrellio\Postgres\Schema\Definitions\Indexes\UniqueDefinition;
24
use Umbrellio\Postgres\Schema\Definitions\Routines\Functions\CreateFunctionDefinition;
25
use Umbrellio\Postgres\Schema\Definitions\Routines\Functions\DropFunctionDefinition;
26
use Umbrellio\Postgres\Schema\Definitions\Routines\Procedures\CreateProcedureDefinition;
27
use Umbrellio\Postgres\Schema\Definitions\Routines\Procedures\DropProcedureDefinition;
28
use Umbrellio\Postgres\Schema\Definitions\Routines\Triggers\CreateTriggerDefinition;
29
use Umbrellio\Postgres\Schema\Definitions\Routines\Triggers\DropTriggerDefinition;
30
use Umbrellio\Postgres\Schema\Definitions\Tables\AttachPartitionDefinition;
31
use Umbrellio\Postgres\Schema\Definitions\Tables\LikeDefinition;
32
use Umbrellio\Postgres\Schema\Definitions\Views\ViewDefinition;
33
use Umbrellio\Postgres\Schema\Types\DateRangeType;
34
use Umbrellio\Postgres\Schema\Types\TsRangeType;
35
use Umbrellio\Postgres\Schema\Types\TsTzRangeType;
36
37
class Blueprint extends BaseBlueprint
38
{
39
    protected $commands = [];
40
41
    /**
42
     * @return AttachPartitionDefinition|Fluent
43
     */
44 4
    public function attachPartition(string $partition): Fluent
45
    {
46 4
        return $this->addCommand('attachPartition', compact('partition'));
47
    }
48
49 1
    public function detachPartition(string $partition): void
50
    {
51 1
        $this->addCommand('detachPartition', compact('partition'));
52
    }
53
54
    /**
55
     * @codeCoverageIgnore
56
     * @return LikeDefinition|Fluent
57
     */
58
    public function like(string $table): Fluent
59
    {
60
        return $this->addCommand('like', compact('table'));
61
    }
62
63
    /**
64
     * @codeCoverageIgnore
65
     */
66
    public function ifNotExists(): Fluent
67
    {
68
        return $this->addCommand('ifNotExists');
69
    }
70
71
    /**
72
     * @param array|string $columns
73
     * @return UniqueDefinition|UniqueBuilder
74
     */
75 13
    public function uniquePartial($columns, ?string $index = null, ?string $algorithm = null): Fluent
76
    {
77 13
        $columns = (array) $columns;
78
79 13
        $index = $index ?: $this->createIndexName('unique', $columns);
80
81 13
        return $this->addExtendedCommand(
82 13
            UniqueBuilder::class,
83 13
            'uniquePartial',
84 13
            compact('columns', 'index', 'algorithm')
85 13
        );
86
    }
87
88 12
    public function dropUniquePartial($index): Fluent
89
    {
90 12
        return $this->dropIndexCommand('dropIndex', 'unique', $index);
91
    }
92
93
    /**
94
     * @param array|string $columns
95
     * @return ExcludeDefinition|ExcludeBuilder
96
     */
97 6
    public function exclude($columns, ?string $index = null): Fluent
98
    {
99 6
        $columns = (array) $columns;
100
101 6
        $index = $index ?: $this->createIndexName('excl', $columns);
102
103 6
        return $this->addExtendedCommand(ExcludeBuilder::class, 'exclude', compact('columns', 'index'));
104
    }
105
106
    /**
107
     * @param array|string $columns
108
     * @return CheckDefinition|CheckBuilder
109
     */
110 3
    public function check($columns, ?string $index = null): Fluent
111
    {
112 3
        $columns = (array) $columns;
113
114 3
        $index = $index ?: $this->createIndexName('chk', $columns);
115
116 3
        return $this->addExtendedCommand(CheckBuilder::class, 'check', compact('columns', 'index'));
117
    }
118
119 1
    public function dropExclude($index): Fluent
120
    {
121 1
        return $this->dropIndexCommand('dropUnique', 'excl', $index);
122
    }
123
124 1
    public function dropCheck($index): Fluent
125
    {
126 1
        return $this->dropIndexCommand('dropUnique', 'chk', $index);
127
    }
128
129
    /**
130
     * @codeCoverageIgnore
131
     */
132
    public function hasIndex($index, bool $unique = false): bool
133
    {
134
        if (is_array($index)) {
135
            $index = $this->createIndexName($unique === false ? 'index' : 'unique', $index);
136
        }
137
138
        return array_key_exists($index, $this->getSchemaManager()->listTableIndexes($this->getTable()));
139
    }
140
141
    /**
142
     * @codeCoverageIgnore
143
     * @return ViewDefinition|Fluent
144
     */
145
    public function createView(string $view, string $select, bool $materialize = false)
146
    {
147
        return $this->addCommand('createView', compact('view', 'select', 'materialize'));
148
    }
149
150
    /**
151
     * @return CreateTriggerDefinition
152
     */
153 2
    public function createTrigger(string $name)
154
    {
155 2
        return $this->addExtendedCommand(CreateTriggerBuilder::class, 'createTrigger', compact('name'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...gger', compact('name')) returns the type Umbrellio\Postgres\Schem...es\CreateTriggerBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...CreateTriggerDefinition.
Loading history...
156
    }
157
158
    /**
159
     * @return CreateFunctionDefinition
160
     */
161 2
    public function createFunction(string $name)
162
    {
163 2
        return $this->addExtendedCommand(CreateFunctionBuilder::class, 'createFunction', compact('name'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...tion', compact('name')) returns the type Umbrellio\Postgres\Schem...s\CreateFunctionBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...reateFunctionDefinition.
Loading history...
164
    }
165
166
    /**
167
     * @return CreateProcedureDefinition
168
     */
169 2
    public function createProcedure(string $name)
170
    {
171 2
        return $this->addExtendedCommand(CreateProcedureBuilder::class, 'createProcedure', compact('name'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...dure', compact('name')) returns the type Umbrellio\Postgres\Schem...\CreateProcedureBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...eateProcedureDefinition.
Loading history...
172
    }
173
174
    /**
175
     * @return DropFunctionDefinition
176
     */
177 2
    public function dropFunction(string $name)
178
    {
179 2
        return $this->addExtendedCommand(DropFunctionBuilder::class, 'dropFunction', compact('name'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...tion', compact('name')) returns the type Umbrellio\Postgres\Schem...nes\DropFunctionBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...\DropFunctionDefinition.
Loading history...
180
    }
181
182
    /**
183
     * @return DropProcedureDefinition
184
     */
185 2
    public function dropProcedure(string $name)
186
    {
187 2
        return $this->addExtendedCommand(DropProcedureBuilder::class, 'dropProcedure', compact('name'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...dure', compact('name')) returns the type Umbrellio\Postgres\Schem...es\DropProcedureBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...DropProcedureDefinition.
Loading history...
188
    }
189
190
    /**
191
     * @codeCoverageIgnore
192
     */
193
    public function dropView(string $view): Fluent
194
    {
195
        return $this->addCommand('dropView', compact('view'));
196
    }
197
198
    /**
199
     * @return DropTriggerDefinition
200
     */
201 2
    public function dropTrigger(string $name, bool $dropDepends = false)
202
    {
203 2
        return $this->addExtendedCommand(DropTriggerBuilder::class, 'dropTrigger', compact('name', 'dropDepends'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addExtende...'name', 'dropDepends')) returns the type Umbrellio\Postgres\Schem...ines\DropTriggerBuilder which is incompatible with the documented return type Umbrellio\Postgres\Schem...s\DropTriggerDefinition.
Loading history...
204
    }
205
206
    /**
207
     * Almost like 'decimal' type, but can be with variable precision (by default)
208
     *
209
     * @return Fluent|ColumnDefinition
210
     */
211 3
    public function numeric(string $column, ?int $precision = null, ?int $scale = null): Fluent
212
    {
213 3
        return $this->addColumn('numeric', $column, compact('precision', 'scale'));
214
    }
215
216
    /**
217
     * @return Fluent|ColumnDefinition
218
     */
219 1
    public function tsrange(string $column): Fluent
220
    {
221 1
        return $this->addColumn(TsRangeType::TYPE_NAME, $column);
222
    }
223
224
    /**
225
     * @return Fluent|ColumnDefinition
226
     */
227 1
    public function tstzrange(string $column): Fluent
228
    {
229 1
        return $this->addColumn(TsTzRangeType::TYPE_NAME, $column);
230
    }
231
232
    /**
233
     * @return Fluent|ColumnDefinition
234
     */
235 1
    public function daterange(string $column): Fluent
236
    {
237 1
        return $this->addColumn(DateRangeType::TYPE_NAME, $column);
238
    }
239
240
    /**
241
     * @codeCoverageIgnore
242
     */
243
    protected function getSchemaManager()
244
    {
245
        /** @scrutinizer ignore-call */
246
        $connection = Schema::getConnection();
247
        $doctrineConnection = DriverManager::getConnection($connection->getConfig());
248
        return $doctrineConnection->getSchemaManager();
249
    }
250
251 29
    private function addExtendedCommand(string $fluent, string $name, array $parameters = []): Fluent
252
    {
253 29
        $command = new $fluent(array_merge(compact('name'), $parameters));
254 29
        $this->commands[] = $command;
255 29
        return $command;
256
    }
257
}
258