Passed
Push — master ( aeb9c0...cd9c67 )
by Dallas
05:37
created

Blueprint::hasIndex()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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

161
        return Schema::/** @scrutinizer ignore-call */ getConnection()->getDoctrineSchemaManager();

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...
162
    }
163
164 22
    private function addExtendedCommand(string $fluent, string $name, array $parameters = []): Fluent
165
    {
166 22
        $command = new $fluent(array_merge(compact('name'), $parameters));
167 22
        $this->commands[] = $command;
168 22
        return $command;
169
    }
170
}
171