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

Blueprint::ifNotExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
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\Definitions\AttachPartitionDefinition;
16
use Umbrellio\Postgres\Schema\Definitions\CheckDefinition;
17
use Umbrellio\Postgres\Schema\Definitions\ExcludeDefinition;
18
use Umbrellio\Postgres\Schema\Definitions\LikeDefinition;
19
use Umbrellio\Postgres\Schema\Definitions\UniqueDefinition;
20
use Umbrellio\Postgres\Schema\Definitions\ViewDefinition;
21
use Umbrellio\Postgres\Schema\Types\DateRangeType;
22
use Umbrellio\Postgres\Schema\Types\TsRangeType;
23
use Umbrellio\Postgres\Schema\Types\TsTzRangeType;
24
25
class Blueprint extends BaseBlueprint
26
{
27
    protected $commands = [];
28
29
    /**
30
     * @return AttachPartitionDefinition|Fluent
31
     */
32 4
    public function attachPartition(string $partition): Fluent
33
    {
34 4
        return $this->addCommand('attachPartition', compact('partition'));
35
    }
36
37 1
    public function detachPartition(string $partition): void
38
    {
39 1
        $this->addCommand('detachPartition', compact('partition'));
40
    }
41
42
    /**
43
     * @codeCoverageIgnore
44
     * @return LikeDefinition|Fluent
45
     */
46
    public function like(string $table): Fluent
47
    {
48
        return $this->addCommand('like', compact('table'));
49
    }
50
51
    /**
52
     * @codeCoverageIgnore
53
     * @return Fluent
54
     */
55
    public function ifNotExists(): Fluent
56
    {
57
        return $this->addCommand('ifNotExists');
58
    }
59
60
    /**
61
     * @param array|string $columns
62
     * @return UniqueDefinition|UniqueBuilder
63
     */
64 13
    public function uniquePartial($columns, ?string $index = null, ?string $algorithm = null): Fluent
65
    {
66 13
        $columns = (array) $columns;
67
68 13
        $index = $index ?: $this->createIndexName('unique', $columns);
69
70 13
        return $this->addExtendedCommand(
71 13
            UniqueBuilder::class,
72 13
            'uniquePartial',
73 13
            compact('columns', 'index', 'algorithm')
74 13
        );
75
    }
76
77 12
    public function dropUniquePartial($index): Fluent
78
    {
79 12
        return $this->dropIndexCommand('dropIndex', 'unique', $index);
80
    }
81
82
    /**
83
     * @param array|string $columns
84
     * @return ExcludeDefinition|ExcludeBuilder
85
     */
86 6
    public function exclude($columns, ?string $index = null): Fluent
87
    {
88 6
        $columns = (array) $columns;
89
90 6
        $index = $index ?: $this->createIndexName('excl', $columns);
91
92 6
        return $this->addExtendedCommand(ExcludeBuilder::class, 'exclude', compact('columns', 'index'));
93
    }
94
95
    /**
96
     * @param array|string $columns
97
     * @return CheckDefinition|CheckBuilder
98
     */
99 3
    public function check($columns, ?string $index = null): Fluent
100
    {
101 3
        $columns = (array) $columns;
102
103 3
        $index = $index ?: $this->createIndexName('chk', $columns);
104
105 3
        return $this->addExtendedCommand(CheckBuilder::class, 'check', compact('columns', 'index'));
106
    }
107
108 1
    public function dropExclude($index): Fluent
109
    {
110 1
        return $this->dropIndexCommand('dropUnique', 'excl', $index);
111
    }
112
113 1
    public function dropCheck($index): Fluent
114
    {
115 1
        return $this->dropIndexCommand('dropUnique', 'chk', $index);
116
    }
117
118
    /**
119
     * @codeCoverageIgnore
120
     */
121
    public function hasIndex($index, bool $unique = false): bool
122
    {
123
        if (is_array($index)) {
124
            $index = $this->createIndexName($unique === false ? 'index' : 'unique', $index);
125
        }
126
127
        return array_key_exists($index, $this->getSchemaManager()->listTableIndexes($this->getTable()));
128
    }
129
130
    /**
131
     * @codeCoverageIgnore
132
     * @return ViewDefinition|Fluent
133
     */
134
    public function createView(string $view, string $select, bool $materialize = false): Fluent
135
    {
136
        return $this->addCommand('createView', compact('view', 'select', 'materialize'));
137
    }
138
139
    /**
140
     * @codeCoverageIgnore
141
     */
142
    public function dropView(string $view): Fluent
143
    {
144
        return $this->addCommand('dropView', compact('view'));
145
    }
146
147
    /**
148
     * Almost like 'decimal' type, but can be with variable precision (by default)
149
     *
150
     * @return Fluent|ColumnDefinition
151
     */
152 3
    public function numeric(string $column, ?int $precision = null, ?int $scale = null): Fluent
153
    {
154 3
        return $this->addColumn('numeric', $column, compact('precision', 'scale'));
155
    }
156
157
    /**
158
     * @return Fluent|ColumnDefinition
159
     */
160 1
    public function tsrange(string $column): Fluent
161
    {
162 1
        return $this->addColumn(TsRangeType::TYPE_NAME, $column);
163
    }
164
165
    /**
166
     * @return Fluent|ColumnDefinition
167
     */
168 1
    public function tstzrange(string $column): Fluent
169
    {
170 1
        return $this->addColumn(TsTzRangeType::TYPE_NAME, $column);
171
    }
172
173
    /**
174
     * @return Fluent|ColumnDefinition
175
     */
176 1
    public function daterange(string $column): Fluent
177
    {
178 1
        return $this->addColumn(DateRangeType::TYPE_NAME, $column);
179
    }
180
181
    /**
182
     * @codeCoverageIgnore
183
     */
184
    protected function getSchemaManager()
185
    {
186
        $connection = Schema::getConnection();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Illuminate\Support\Facades\Schema. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
        /** @scrutinizer ignore-call */ 
187
        $connection = Schema::getConnection();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
        $doctrineConnection = DriverManager::getConnection($connection->getConfig());
188
        return $doctrineConnection->getSchemaManager();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::getSchemaManager() has been deprecated: Use {@see createSchemaManager()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

188
        return /** @scrutinizer ignore-deprecated */ $doctrineConnection->getSchemaManager();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
189
    }
190
191 22
    private function addExtendedCommand(string $fluent, string $name, array $parameters = []): Fluent
192
    {
193 22
        $command = new $fluent(array_merge(compact('name'), $parameters));
194 22
        $this->commands[] = $command;
195 22
        return $command;
196
    }
197
}
198