Passed
Push — refactor/improve-static-analys... ( efcf20...37f12c )
by Bas
03:04
created

Builder   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 41
c 2
b 1
f 0
dl 0
loc 203
ccs 42
cts 48
cp 0.875
rs 10
wmc 19

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getAllTables() 0 3 1
A hasTable() 0 4 1
A hasColumns() 0 11 1
A dropIfExists() 0 5 2
A drop() 0 3 1
A createDatabase() 0 3 1
A rename() 0 3 1
A __call() 0 3 1
A getConnection() 0 3 1
A dropAllTables() 0 6 2
A hasColumn() 0 7 2
A handleExceptionsAsQueryExceptions() 0 6 2
A dropDatabaseIfExists() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Schema;
6
7
use ArangoClient\Exceptions\ArangoException;
8
use ArangoClient\Schema\SchemaManager;
9
use Closure;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Fluent;
12
use LaravelFreelancerNL\Aranguent\Connection;
13
use LaravelFreelancerNL\Aranguent\Exceptions\QueryException;
14
use LaravelFreelancerNL\Aranguent\Schema\Concerns\HandlesAnalyzers;
15
use LaravelFreelancerNL\Aranguent\Schema\Concerns\HandlesViews;
16
use LaravelFreelancerNL\Aranguent\Schema\Concerns\UsesBlueprints;
17
18
class Builder extends \Illuminate\Database\Schema\Builder
19
{
20
    use HandlesAnalyzers;
21
    use HandlesViews;
0 ignored issues
show
Bug introduced by
The trait LaravelFreelancerNL\Aran...a\Concerns\HandlesViews requires the property $name which is not provided by LaravelFreelancerNL\Aranguent\Schema\Builder.
Loading history...
22
    use UsesBlueprints;
23
24
    /**
25
     * The database connection instance.
26
     *
27
     * @var Connection
28
     */
29
    protected $connection;
30
31
    protected SchemaManager $schemaManager;
32
33
    /**
34
     * The schema grammar instance.
35
     *
36
     * @var Grammar
37
     */
38
    protected $grammar;
39
40
    /**
41
     * Create a new database Schema manager.
42
     *
43
     * Builder constructor.
44
     */
45 36
    public function __construct(Connection $connection)
46
    {
47 36
        $this->connection = $connection;
48
49 36
        $this->grammar = $connection->getSchemaGrammar();
50
51 36
        $this->schemaManager = $connection->getArangoClient()->schema();
52
    }
53
54
    /**
55
     * Determine if the given table exists.
56
     *
57
     * @param  string  $table
58
     * @return bool
59
     */
60 4
    public function hasTable($table)
61
    {
62 4
        return $this->handleExceptionsAsQueryExceptions(function () use ($table) {
63 4
            return $this->schemaManager->hasCollection($table);
64 4
        });
65
    }
66
67
    /**
68
     * @throws ArangoException
69
     */
70
    public function dropIfExists($table): void
71
    {
72
        $tableExists = $this->hasTable($table);
73
        if ($tableExists) {
74
            $this->drop($table);
75
        }
76
    }
77
78
    /**
79
     * Get all the tables for the database; excluding ArangoDB system collections
80
     *
81
     * @return array<mixed>
82
     *
83
     * @throws ArangoException
84
     */
85 2
    public function getAllTables(): array
86
    {
87 2
        return $this->schemaManager->getCollections(true);
88
    }
89
90
    /**
91
     * Rename a table (collection).
92
     *
93
     * @param  string  $from
94
     * @param  string  $to
95
     *
96
     * @throws ArangoException
97
     */
98 1
    public function rename($from, $to): bool
99
    {
100 1
        return (bool) $this->schemaManager->renameCollection($from, $to);
101
    }
102
103
    /**
104
     * Drop a table (collection) from the schema.
105
     *
106
     * @throws ArangoException
107
     */
108 1
    public function drop($table)
109
    {
110 1
        $this->schemaManager->deleteCollection($table);
111
    }
112
113
    /**
114
     * Drop all tables (collections) from the schema.
115
     *
116
     * @throws ArangoException
117
     */
118 2
    public function dropAllTables(): void
119
    {
120 2
        $collections = $this->getAllTables();
121
122 2
        foreach ($collections as $name) {
123 2
            $this->schemaManager->deleteCollection($name->name);
124
        }
125
    }
126
127
    /**
128
     * Determine if the given table has a given column.
129
     *
130
     * @param  string  $table
131
     * @param  string  $column
132
     * @return bool
133
     */
134 1
    public function hasColumn($table, $column)
135
    {
136 1
        if (is_string($column)) {
0 ignored issues
show
introduced by
The condition is_string($column) is always true.
Loading history...
137 1
            $column = [$column];
138
        }
139
140 1
        return $this->hasColumns($table, $column);
141
    }
142
143
    /**
144
     * Determine if the given table has given columns.
145
     *
146
     * @param  string  $table
147
     * @return bool
148
     */
149 1
    public function hasColumns($table, array $columns)
150
    {
151 1
        $parameters = [];
152 1
        $parameters['name'] = 'hasAttribute';
153 1
        $parameters['handler'] = 'aql';
154 1
        $parameters['columns'] = $columns;
155
156 1
        $command = new Fluent($parameters);
157 1
        $compilation = $this->grammar->compileHasColumn($table, $command);
158
159 1
        return $this->connection->statement($compilation['aql']);
160
    }
161
162
163
    /**
164
     * Create a database in the schema.
165
     *
166
     * @param  string  $name
167
     * @return bool
168
     *
169
     * @throws ArangoException
170
     */
171 2
    public function createDatabase($name)
172
    {
173 2
        return $this->schemaManager->createDatabase($name);
174
    }
175
176
    /**
177
     * Drop a database from the schema if the database exists.
178
     *
179
     * @param  string  $name
180
     * @return bool
181
     *
182
     * @throws ArangoException
183
     */
184 2
    public function dropDatabaseIfExists($name)
185
    {
186 2
        if ($this->schemaManager->hasDatabase($name)) {
187 1
            return $this->schemaManager->deleteDatabase($name);
188
        }
189
190 1
        return true;
191
    }
192
193
    /**
194
     * Get the database connection instance.
195
     *
196
     * @return Connection
197
     */
198 1
    public function getConnection()
199
    {
200 1
        return $this->connection;
201
    }
202
203
    /**
204
     * @throws QueryException
205
     */
206 8
    protected function handleExceptionsAsQueryExceptions(Closure $callback): mixed
207
    {
208
        try {
209 8
            return $callback();
210 1
        } catch (\Exception $e) {
211 1
            throw new QueryException($this->connection->getName(), $e->getMessage(), [], $e);
212
        }
213
    }
214
215
    /**
216
     * Silently catch the use of unsupported builder methods.
217
     */
218
    public function __call(string $method, mixed $args): void
219
    {
220
        Log::warning("The Aranguent Schema Builder doesn't support method '$method'\n");
221
    }
222
}
223