Passed
Push — next ( aa8376...09a952 )
by Bas
02:36
created

Builder::createDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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