Test Setup Failed
Pull Request — master (#10)
by
unknown
01:32
created

Builder::dropIfExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Schema;
4
5
use ArangoDBClient\View;
6
use Closure;
7
use Illuminate\Support\Fluent;
8
use LaravelFreelancerNL\Aranguent\Connection;
9
10
class Builder
11
{
12
    /**
13
     * The database connection instance.
14
     *
15
     * @var \Illuminate\Database\Connection
16
     */
17
    protected $connection;
18
19
    protected $collectionHandler;
20
21
    protected $graphHandler;
22
23
    protected $viewHandler;
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
     * @param Connection $connection
44
     */
45
    public function __construct(Connection $connection)
46
    {
47
        $this->connection = $connection;
48
49
        $this->grammar = $connection->getSchemaGrammar();
50
51
        $this->collectionHandler = $connection->getCollectionHandler();
52
53
        $this->graphHandler = $connection->getGraphHandler();
54
55
        $this->viewHandler = $connection->getViewHandler();
56
    }
57
58
    /**
59
     * Create a new collection on the schema.
60
     *
61
     * @param  string    $collection
62
     * @param  Closure  $callback
63
     * @param  array $config
64
     * @return void
65
     */
66
    public function create($collection, Closure $callback, $config = [])
67
    {
68
        $this->build(tap($this->createBlueprint($collection), function ($blueprint) use ($callback, $config) {
69
            $blueprint->create($config);
70
71
            $callback($blueprint);
72
        }));
73
    }
74
75
    /**
76
     * Create a new command set with a Closure.
77
     *
78
     * @param  string  $collection
79
     * @param  \Closure|null  $callback
80
     * @return Blueprint
81
     */
82
    protected function createBlueprint($collection, Closure $callback = null)
83
    {
84
        //Prefixes are unnamed in ArangoDB
85
        $prefix = null;
86
87
        if (isset($this->resolver)) {
88
            return call_user_func($this->resolver, $collection, $callback, $prefix);
89
        }
90
91
        return new Blueprint($collection, $this->collectionHandler, $callback, $prefix);
92
    }
93
94
    protected function build(Blueprint $blueprint)
95
    {
96
        $blueprint->build($this->connection, $this->grammar);
97
    }
98
99
    /**
100
     * Set the Schema Blueprint resolver callback.
101
     *
102
     * @param  Closure  $resolver
103
     * @return void
104
     */
105
    public function blueprintResolver(Closure $resolver)
106
    {
107
        $this->resolver = $resolver;
108
    }
109
110
    /**
111
     * Alias for table.
112
     *
113
     * @param  string    $collection
114
     * @param  Closure  $callback
115
     * @return void
116
     */
117
    public function collection($collection, Closure $callback)
118
    {
119
        $this->collection($collection, $callback);
120
    }
121
122
    /**
123
     * Modify a table's schema.
124
     *
125
     * @param  string    $table
126
     * @param  Closure  $callback
127
     * @return void
128
     */
129
    public function table($table, Closure $callback)
130
    {
131
        $this->build($this->createBlueprint($table, $callback));
132
    }
133
134
    /**
135
     * Drop a collection from the schema.
136
     *
137
     * @param string $collection
138
     * @return void
139
     * @throws \ArangoDBClient\Exception
140
     */
141
    public function drop($collection)
142
    {
143
        $this->collectionHandler->drop($collection);
144
    }
145
146
    public function dropAllCollections()
147
    {
148
        $collections = $this->getAllCollections();
149
150
        foreach ($collections as $key => $name) {
151
            $this->collectionHandler->drop($name);
152
        }
153
    }
154
155
    /**
156
     * Alias for dropAllCollections.
157
     *
158
     * @return void
159
     */
160
    public function dropAllTables()
161
    {
162
        $this->dropAllCollections();
163
    }
164
165
    /**
166
     * @param $collection
167
     * @throws \ArangoDBClient\Exception
168
     */
169
    public function dropIfExists($collection)
170
    {
171
        if ($collections = $this->hasCollection($collection)) {
0 ignored issues
show
Unused Code introduced by
$collections is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
172
            $this->drop($collection);
173
        }
174
    }
175
176
    /**
177
     * Get all of the collection names for the database.
178
     *
179
     * @param array $options
180
     * @return array
181
     * @throws \ArangoDBClient\ClientException
182
     * @throws \ArangoDBClient\Exception
183
     */
184
    protected function getAllCollections(array $options = [])
185
    {
186
        if (! isset($options['excludeSystem'])) {
187
            $options['excludeSystem'] = true;
188
        }
189
190
        return $this->collectionHandler->getAllCollections($options);
191
    }
192
193
    /**
194
     * Get all of the table names for the database.
195
     * Alias for getAllCollections().
196
     *
197
     * @param array $options
198
     * @return array
199
     * @throws \ArangoDBClient\ClientException
200
     * @throws \ArangoDBClient\Exception
201
     */
202
    protected function getAllTables(array $options = [])
203
    {
204
        return $this->getAllCollections($options);
205
    }
206
207
    /**
208
     * @param string $collection
209
     * @return \ArangoDBClient\Collection
210
     * @throws \ArangoDBClient\Exception
211
     */
212
    protected function getCollection($collection)
213
    {
214
        return $this->collectionHandler->get($collection);
215
    }
216
217
    /**
218
     * Rename a collection.
219
     *
220
     * @param $from
221
     * @param $to
222
     * @return bool
223
     * @throws \ArangoDBClient\Exception
224
     */
225
    public function rename($from, $to)
226
    {
227
        return $this->collectionHandler->rename($from, $to);
228
    }
229
230
    /**
231
     * Determine if the given collection exists.
232
     *
233
     * @param string $collection
234
     * @return bool
235
     * @throws \ArangoDBClient\Exception
236
     */
237
    public function hasCollection(string $collection)
238
    {
239
        return $this->collectionHandler->has($collection);
240
    }
241
242
    /**
243
     * Alias for hasCollection.
244
     *
245
     * @param string $table
246
     * @return bool
247
     * @throws \ArangoDBClient\Exception
248
     */
249
    public function hasTable($table)
250
    {
251
        return $this->hasCollection($table);
252
    }
253
254
    /**
255
     * Check if any document in the collection has the attribute.
256
     *
257
     * @param string $collection
258
     * @param string $attribute
259
     * @return bool
260
     */
261
    public function hasAttribute($collection, $attribute)
262
    {
263
        if (is_string($attribute)) {
264
            $attribute = [$attribute];
265
        }
266
267
        return $this->hasAttributes($collection, $attribute);
268
    }
269
270
    /**
271
     * Check if any document in the collection has the attribute.
272
     *
273
     * @param string $collection
274
     * @param array $attributes
275
     * @return bool
276
     */
277
    public function hasAttributes($collection, $attributes)
278
    {
279
        $parameters['name'] = 'hasAttribute';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
280
        $parameters['handler'] = 'aql';
281
        $parameters['attribute'] = $attributes;
282
283
        $command = new Fluent($parameters);
284
        $compilation = $this->grammar->compileHasAttribute($collection, $command);
285
286
        return $this->connection->statement($compilation['aql']);
287
    }
288
289
    /**
290
     * Alias for hasAttribute.
291
     *
292
     * @param  string  $table
293
     * @param  string  $column
294
     * @return bool
295
     */
296
    public function hasColumn($table, $column)
297
    {
298
        return $this->hasAttribute($table, $column);
299
    }
300
301
    /**
302
     * Alias for hasAttributes.
303
     *
304
     * @param  string  $table
305
     * @param  array  $columns
306
     * @return bool
307
     */
308
    public function hasColumns($table, $columns)
309
    {
310
        return $this->hasAttributes($table, $columns);
311
    }
312
313
    /**
314
     * get information about the collection.
315
     *
316
     * @param $collection
317
     * @return mixed
318
     */
319
    public function getCollectionInfo($collection)
320
    {
321
        return $this->figures($collection);
0 ignored issues
show
Documentation Bug introduced by
The method figures does not exist on object<LaravelFreelancer...anguent\Schema\Builder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
322
    }
323
324
    /**
325
     * @param string $name
326
     * @param array $properties
327
     * @param string $type
328
     * @throws \ArangoDBClient\ClientException
329
     * @throws \ArangoDBClient\Exception
330
     */
331
    public function createView($name, array $properties, $type = 'arangosearch')
332
    {
333
        $view = new View($name, $type);
0 ignored issues
show
Documentation introduced by
$name is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
334
335
        $this->viewHandler->create($view);
336
        $this->viewHandler->setProperties($view, $properties);
337
    }
338
339
    /**
340
     * @param string $name
341
     * @return mixed
342
     * @throws \ArangoDBClient\Exception
343
     */
344
    public function getView(string $name)
345
    {
346
        return $this->viewHandler->properties($name);
347
    }
348
349
    /**
350
     * @param string $name
351
     * @param array $properties
352
     * @throws \ArangoDBClient\ClientException
353
     * @throws \ArangoDBClient\Exception
354
     */
355
    public function editView($name, array $properties)
356
    {
357
        $this->viewHandler->setProperties($name, $properties);
358
    }
359
360
    /**
361
     * @param string $from
362
     * @param string $to
363
     * @throws \ArangoDBClient\Exception
364
     */
365
    public function renameView(string $from, string $to)
366
    {
367
        $this->viewHandler->rename($from, $to);
368
    }
369
370
    /**
371
     * @param string $name
372
     * @throws \ArangoDBClient\ClientException
373
     * @throws \ArangoDBClient\Exception
374
     */
375
    public function dropView(string $name)
376
    {
377
        $this->viewHandler->drop($name);
378
    }
379
380
    /**
381
     * Get the database connection instance.
382
     *
383
     * @return Connection
384
     */
385
    public function getConnection()
386
    {
387
        return $this->connection;
388
    }
389
390
    /**
391
     * Silently catch the use of unsupported builder methods.
392
     *
393
     * @param $method
394
     * @param $args
395
     * @return null
396
     */
397
    public function __call($method, $args)
398
    {
399
        error_log("The Aranguent Schema Builder doesn't support method '$method'\n");
400
    }
401
}
402