Passed
Pull Request — master (#38)
by Bas
17:19
created

Builder::hasAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Schema;
4
5
use ArangoDBClient\ClientException;
6
use ArangoDBClient\Collection;
7
use ArangoDBClient\Exception;
8
use ArangoDBClient\View;
9
use Closure;
10
use Illuminate\Support\Fluent;
11
use LaravelFreelancerNL\Aranguent\Connection;
12
13
class Builder
14
{
15
    /**
16
     * The database connection instance.
17
     *
18
     * @var \Illuminate\Database\Connection
19
     */
20
    protected $connection;
21
22
    protected $collectionHandler;
23
24
    protected $graphHandler;
25
26
    protected $viewHandler;
27
28
    /**
29
     * The schema grammar instance.
30
     *
31
     * @var Grammar
32
     */
33
    protected $grammar;
34
35
    /**
36
     * The Blueprint resolver callback.
37
     *
38
     * @var \Closure
39
     */
40
    protected $resolver;
41
42
    /**
43
     * Create a new database Schema manager.
44
     *
45
     * Builder constructor.
46
     *
47
     * @param Connection $connection
48
     */
49
    public function __construct(Connection $connection)
50
    {
51
        $this->connection = $connection;
52
53
        $this->grammar = $connection->getSchemaGrammar();
54
55
        $this->collectionHandler = $connection->getCollectionHandler();
56
57
        $this->graphHandler = $connection->getGraphHandler();
58
59
        $this->viewHandler = $connection->getViewHandler();
60
    }
61
62
    /**
63
     * Create a new collection on the schema.
64
     *
65
     * @param string  $collection
66
     * @param Closure $callback
67
     * @param array   $config
68
     *
69
     * @return void
70
     */
71
    public function create($collection, Closure $callback, $config = [])
72
    {
73
        $this->build(tap($this->createBlueprint($collection), function ($blueprint) use ($callback, $config) {
74
            $blueprint->create($config);
75
76
            $callback($blueprint);
77
        }));
78
    }
79
80
    /**
81
     * Create a new command set with a Closure.
82
     *
83
     * @param string        $collection
84
     * @param \Closure|null $callback
85
     *
86
     * @return Blueprint
87
     */
88
    protected function createBlueprint($collection, Closure $callback = null)
89
    {
90
        //Prefixes are unnamed in ArangoDB
91
        $prefix = null;
92
93
        if (isset($this->resolver)) {
94
            return call_user_func($this->resolver, $collection, $callback, $prefix);
95
        }
96
97
        return new Blueprint($collection, $this->collectionHandler, $callback, $prefix);
98
    }
99
100
    protected function build(Blueprint $blueprint)
101
    {
102
        $blueprint->build($this->connection, $this->grammar);
103
    }
104
105
    /**
106
     * Set the Schema Blueprint resolver callback.
107
     *
108
     * @param Closure $resolver
109
     *
110
     * @return void
111
     */
112
    public function blueprintResolver(Closure $resolver)
113
    {
114
        $this->resolver = $resolver;
115
    }
116
117
    /**
118
     * Alias for table.
119
     *
120
     * @param string  $collection
121
     * @param Closure $callback
122
     *
123
     * @return void
124
     */
125
    public function collection($collection, Closure $callback)
126
    {
127
        $this->collection($collection, $callback);
128
    }
129
130
    /**
131
     * Modify a table's schema.
132
     *
133
     * @param string  $table
134
     * @param Closure $callback
135
     *
136
     * @return void
137
     */
138
    public function table($table, Closure $callback)
139
    {
140
        $this->build($this->createBlueprint($table, $callback));
141
    }
142
143
    /**
144
     * Drop a collection from the schema.
145
     *
146
     * @param  string  $collection
147
     *
148
     * @return void
149
     */
150
    public function drop($collection)
151
    {
152
        $this->collectionHandler->drop($collection);
153
    }
154
155
    public function dropAllCollections()
156
    {
157
        $collections = $this->getAllCollections();
158
159
        foreach ($collections as $name) {
160
            $this->collectionHandler->drop($name['name']);
161
        }
162
    }
163
164
    /**
165
     * Alias for dropAllCollections.
166
     *
167
     * @return void
168
     */
169
    public function dropAllTables()
170
    {
171
        $this->dropAllCollections();
172
    }
173
174
    /**
175
     * @param $collection
176
     *
177
     * @throws Exception
178
     */
179
    public function dropIfExists($collection)
180
    {
181
        $collections = $this->hasCollection($collection);
182
        if ($collections) {
183
            $this->drop($collection);
184
        }
185
    }
186
187
    /**
188
     * Get all of the collection names for the database.
189
     *
190
     * @param  array  $options
191
     *
192
     * @return array
193
     */
194
    public function getAllCollections(array $options = [])
195
    {
196
        if (!isset($options['excludeSystem'])) {
197
            $options['excludeSystem'] = true;
198
        }
199
200
        return $this->collectionHandler->getAllCollections($options);
201
    }
202
203
    /**
204
     * Get all of the table names for the database.
205
     * Alias for getAllCollections().
206
     *
207
     * @param  array  $options
208
     *
209
     * @return array
210
     */
211
    protected function getAllTables(array $options = [])
212
    {
213
        return $this->getAllCollections($options);
214
    }
215
216
    /**
217
     * @param  string  $collection
218
     *
219
     * @return Collection
220
     */
221
    protected function getCollection($collection)
222
    {
223
        return $this->collectionHandler->get($collection);
224
    }
225
226
    /**
227
     * Rename a collection.
228
     *
229
     * @param $from
230
     * @param $to
231
     *
232
     * @throws Exception
233
     *
234
     * @return bool
235
     */
236
    public function rename($from, $to)
237
    {
238
        return $this->collectionHandler->rename($from, $to);
239
    }
240
241
    /**
242
     * Determine if the given collection exists.
243
     *
244
     * @param string $collection
245
     *
246
     * @throws Exception
247
     *
248
     * @return bool
249
     */
250
    public function hasCollection(string $collection)
251
    {
252
        return $this->collectionHandler->has($collection);
253
    }
254
255
    /**
256
     * Alias for hasCollection.
257
     *
258
     * @param string $table
259
     *
260
     * @throws Exception
261
     *
262
     * @return bool
263
     */
264
    public function hasTable($table)
265
    {
266
        return $this->hasCollection($table);
267
    }
268
269
    /**
270
     * Check if any document in the collection has the attribute.
271
     *
272
     * @param string $collection
273
     * @param string $attribute
274
     *
275
     * @return bool
276
     */
277
    public function hasAttribute($collection, $attribute)
278
    {
279
        if (is_string($attribute)) {
0 ignored issues
show
introduced by
The condition is_string($attribute) is always true.
Loading history...
280
            $attribute = [$attribute];
281
        }
282
283
        return $this->hasAttributes($collection, $attribute);
284
    }
285
286
    /**
287
     * Check if any document in the collection has the attribute.
288
     *
289
     * @param string $collection
290
     * @param array  $attributes
291
     *
292
     * @return bool
293
     */
294
    public function hasAttributes($collection, $attributes)
295
    {
296
        $parameters = [];
297
        $parameters['name'] = 'hasAttribute';
298
        $parameters['handler'] = 'aql';
299
        $parameters['attribute'] = $attributes;
300
301
        $command = new Fluent($parameters);
302
        $compilation = $this->grammar->compileHasAttribute($collection, $command);
303
304
        return $this->connection->statement($compilation['aql']);
305
    }
306
307
    /**
308
     * Alias for hasAttribute.
309
     *
310
     * @param string $table
311
     * @param string $column
312
     *
313
     * @return bool
314
     */
315
    public function hasColumn($table, $column)
316
    {
317
        return $this->hasAttribute($table, $column);
318
    }
319
320
    /**
321
     * Alias for hasAttributes.
322
     *
323
     * @param string $table
324
     * @param array  $columns
325
     *
326
     * @return bool
327
     */
328
    public function hasColumns($table, $columns)
329
    {
330
        return $this->hasAttributes($table, $columns);
331
    }
332
333
    /**
334
     * @param string $name
335
     * @param array  $properties
336
     * @param string $type
337
     *
338
     * @throws ClientException
339
     * @throws Exception
340
     */
341
    public function createView($name, array $properties, $type = 'arangosearch')
342
    {
343
        $view = new View($name, $type);
344
345
        $this->viewHandler->create($view);
346
        $this->viewHandler->setProperties($view, $properties);
347
    }
348
349
    /**
350
     * @param string $name
351
     *
352
     * @throws Exception
353
     *
354
     * @return mixed
355
     */
356
    public function getView(string $name)
357
    {
358
        return $this->viewHandler->properties($name);
359
    }
360
361
    /**
362
     * @param  string  $name
363
     * @param  array  $properties
364
     */
365
    public function editView($name, array $properties)
366
    {
367
        $this->viewHandler->setProperties($name, $properties);
368
    }
369
370
    /**
371
     * @param  string  $from
372
     * @param  string  $to
373
     *
374
     */
375
    public function renameView(string $from, string $to)
376
    {
377
        $this->viewHandler->rename($from, $to);
378
    }
379
380
    /**
381
     * @param  string  $name
382
     */
383
    public function dropView(string $name)
384
    {
385
        $this->viewHandler->drop($name);
386
    }
387
388
    /**
389
     * Get the database connection instance.
390
     *
391
     * @return Connection
392
     */
393
    public function getConnection()
394
    {
395
        return $this->connection;
396
    }
397
398
    /**
399
     * Silently catch the use of unsupported builder methods.
400
     *
401
     * @param $method
402
     * @param $args
403
     *
404
     * @return null
405
     */
406
    public function __call($method, $args)
407
    {
408
        error_log("The Aranguent Schema Builder doesn't support method '$method'\n");
409
    }
410
}
411