Passed
Push — next ( 9707a2...aa8376 )
by Bas
03:34
created

Builder::getTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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