Passed
Push — next ( 480502...54a2e9 )
by Bas
12:53
created

Builder::dropViewIfExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 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\Database\Connection as IlluminateConnection;
11
use Illuminate\Support\Facades\Log;
12
use Illuminate\Support\Fluent;
13
use LaravelFreelancerNL\Aranguent\Connection;
14
use LaravelFreelancerNL\Aranguent\QueryException;
15
use LaravelFreelancerNL\Aranguent\Schema\Concerns\UsesBlueprints;
16
use stdClass;
17
18
class Builder extends \Illuminate\Database\Schema\Builder
19
{
20
    use UsesBlueprints;
21
22
    /**
23
     * The database connection instance.
24
     *
25
     * @var IlluminateConnection
26
     */
27
    protected $connection;
28
29
    protected SchemaManager $schemaManager;
30
31
    /**
32
     * The schema grammar instance.
33
     *
34
     * @var Grammar
35
     */
36
    protected $grammar;
37
38
    /**
39
     * Create a new database Schema manager.
40
     *
41
     * Builder constructor.
42
     *
43
     * @param Connection $connection
44
     */
45 148
    public function __construct(Connection $connection)
46
    {
47 148
        $this->connection = $connection;
48
49 148
        $this->grammar = $connection->getSchemaGrammar();
50
51 148
        $this->schemaManager = $connection->getArangoClient()->schema();
52 148
    }
53
54
    /**
55
     * Determine if the given table exists.
56
     *
57
     * @param string $table
58
     * @return bool
59
     */
60 132
    public function hasTable($table)
61
    {
62 132
        return $this->handleExceptionsAsQueryExceptions(function () use ($table) {
63 132
            return $this->schemaManager->hasCollection($table);
64 132
        });
65
    }
66
67
68
    /**
69
     * @throws ArangoException
70
     */
71 127
    public function dropIfExists($table): void
72
    {
73 127
        $tableExists = $this->hasTable($table);
74 127
        if ($tableExists) {
75 127
            $this->drop($table);
76
        }
77 127
    }
78
79
    /**
80
     * Get all the tables for the database; excluding ArangoDB system collections
81
     *
82
     * @return array<mixed>
83
     * @throws ArangoException
84
     */
85 1
    public function getAllTables(): array
86
    {
87 1
        return $this->schemaManager->getCollections(true);
88
    }
89
90
    /**
91
     * Rename a table (collection).
92
     *
93
     * @param $from
94
     * @param $to
95
     *
96
     * @return bool
97
     * @throws ArangoException
98
     *
99
     */
100 1
    public function rename($from, $to)
101
    {
102 1
        return (bool) $this->schemaManager->renameCollection($from, $to);
103
    }
104
105
    /**
106
     * Drop a table (collection) from the schema.
107
     *
108
     * @throws ArangoException
109
     */
110 127
    public function drop($table)
111
    {
112 127
        $this->schemaManager->deleteCollection($table);
113 127
    }
114
115
    /**
116
     * Drop all tables (collections) from the schema.
117
     *
118
     * @throws ArangoException
119
     */
120 1
    public function dropAllTables(): void
121
    {
122 1
        $collections = $this->getAllTables();
123
124 1
        foreach ($collections as $name) {
125 1
            $this->schemaManager->deleteCollection($name->name);
126
        }
127 1
    }
128
129
    /**
130
     * Determine if the given table has a given column.
131
     *
132
     * @param  string  $table
133
     * @param  string  $column
134
     * @return bool
135
     */
136 1
    public function hasColumn($table, $column)
137
    {
138 1
        if (is_string($column)) {
0 ignored issues
show
introduced by
The condition is_string($column) is always true.
Loading history...
139 1
            $column = [$column];
140
        }
141
142 1
        return $this->hasColumns($table, $column);
143
    }
144
145
    /**
146
     * Determine if the given table has given columns.
147
     *
148
     * @param  string  $table
149
     * @param  array  $columns
150
     * @return bool
151
     */
152 1
    public function hasColumns($table, array $columns)
153
    {
154 1
        $parameters = [];
155 1
        $parameters['name'] = 'hasAttribute';
156 1
        $parameters['handler'] = 'aql';
157 1
        $parameters['attribute'] = $columns;
158
159 1
        $command = new Fluent($parameters);
160 1
        $compilation = $this->grammar->compileHasAttribute($table, $command);
161
162 1
        return $this->connection->statement($compilation['aql']);
163
    }
164
165
    /**
166
     * @param  array<mixed> $properties
167
     * @throws ArangoException
168
     */
169 135
    public function createView(string $name, array $properties, string $type = 'arangosearch')
170
    {
171 135
        $view = $properties;
172 135
        $view['name'] = $name;
173 135
        $view['type'] = $type;
174
175 135
        $this->schemaManager->createView($view);
176 135
    }
177
178
    /**
179
     * @param  string  $name
180
     *
181
     * @return mixed
182
     * @throws ArangoException
183
     */
184 1
    public function getView(string $name): stdClass
185
    {
186 1
        return $this->schemaManager->getView($name);
187
    }
188
189 129
    public function hasView(string $view): bool
190
    {
191 129
        return $this->handleExceptionsAsQueryExceptions(function () use ($view) {
192 129
            return $this->schemaManager->hasView($view);
193 129
        });
194
    }
195
196
    /**
197
     * @throws ArangoException
198
     */
199 1
    public function getAllViews(): array
200
    {
201 1
        return $this->schemaManager->getViews();
202
    }
203
204
    /**
205
     * @param  string  $name
206
     * @param  array  $properties
207
     * @throws ArangoException
208
     */
209 1
    public function editView(string $name, array $properties)
210
    {
211 1
        $this->schemaManager->updateView($name, $properties);
212 1
    }
213
214
    /**
215
     * @param  string  $from
216
     * @param  string  $to
217
     *
218
     * @throws ArangoException
219
     */
220 1
    public function renameView(string $from, string $to)
221
    {
222 1
        $this->schemaManager->renameView($from, $to);
223 1
    }
224
225
    /**
226
     * @param  string  $name
227
     * @throws ArangoException
228
     */
229 1
    public function dropView(string $name)
230
    {
231 1
        $this->schemaManager->deleteView($name);
232 1
    }
233
234
    /**
235
     * @param string $name
236
     * @return bool
237
     * @throws ArangoException
238
     */
239 129
    public function dropViewIfExists(string $name): bool
240
    {
241 129
        if ($this->hasView($name)) {
242 127
            $this->schemaManager->deleteView($name);
243
        }
244
245 129
        return true;
246
    }
247
248
    /**
249
     * Drop all views from the schema.
250
     *
251
     * @throws ArangoException
252
     */
253 1
    public function dropAllViews(): void
254
    {
255 1
        $views = $this->schemaManager->getViews();
256
257 1
        foreach ($views as $view) {
258 1
            $this->schemaManager->deleteView($view->name);
259
        }
260 1
    }
261
262
    /**
263
     * Create a database in the schema.
264
     *
265
     * @param string $name
266
     * @return bool
267
     *
268
     * @throws ArangoException
269
     */
270 2
    public function createDatabase($name)
271
    {
272 2
        return $this->schemaManager->createDatabase($name);
273
    }
274
275
    /**
276
     * Drop a database from the schema if the database exists.
277
     *
278
     * @param string $name
279
     * @return bool
280
     *
281
     * @throws ArangoException
282
     */
283 2
    public function dropDatabaseIfExists($name)
284
    {
285 2
        if ($this->schemaManager->hasDatabase($name)) {
286 1
            return $this->schemaManager->deleteDatabase($name);
287
        }
288
289 1
        return true;
290
    }
291
292
293
    /**
294
     * Get the database connection instance.
295
     *
296
     * @return Connection
297
     */
298 1
    public function getConnection()
299
    {
300 1
        return $this->connection;
301
    }
302
303
    /**
304
     * @throws QueryException
305
     */
306 134
    protected function handleExceptionsAsQueryExceptions(Closure $callback): mixed
307
    {
308
        try {
309 134
            return $callback();
310 2
        } catch (\Exception $e) {
311 2
            throw new QueryException($e->getMessage(), [], $e);
312
        }
313
    }
314
315
    /**
316
     * Silently catch the use of unsupported builder methods.
317
     */
318 1
    public function __call(string $method, mixed $args): void
319
    {
320 1
        Log::warning("The Aranguent Schema Builder doesn't support method '$method'\n");
321 1
    }
322
}
323