Passed
Push — master ( ad651e...c5dde6 )
by Dallas
05:37
created

Blueprint   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 34
c 1
b 0
f 0
dl 0
loc 154
ccs 49
cts 49
cp 1
rs 10

19 Methods

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

170
        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...
171
    }
172
173 22
    private function addExtendedCommand(string $fluent, string $name, array $parameters = []): Fluent
174
    {
175 22
        $command = new $fluent(array_merge(compact('name'), $parameters));
176 22
        $this->commands[] = $command;
177 22
        return $command;
178
    }
179
}
180