Passed
Push — next ( 3b41f7...f014dd )
by Bas
02:34
created

Builder::createBlueprint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Builder::hasTable() 0 4 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 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
     * The Blueprint resolver callback.
40
     *
41
     * @var Closure
42
     */
43
    protected $resolver;
44
45
    /**
46
     * Create a new database Schema manager.
47
     *
48
     * Builder constructor.
49
     *
50
     * @param Connection $connection
51
     */
52 141
    public function __construct(Connection $connection)
53
    {
54 141
        $this->connection = $connection;
55
56 141
        $this->grammar = $connection->getSchemaGrammar();
57
58 141
        $this->schemaManager = $connection->getArangoClient()->schema();
59 141
    }
60
61
    /**
62
     * Determine if the given table exists.
63
     *
64
     * @param string $table
65
     * @return bool
66
     */
67 125
    public function hasTable($table)
68
    {
69 125
        return $this->handleExceptionsAsQueryExceptions(function () use ($table) {
70 125
            return $this->schemaManager->hasCollection($table);
71 125
        });
72
    }
73
74
75
    /**
76
     * @throws ArangoException
77
     */
78 120
    public function dropIfExists($table): void
79
    {
80 120
        $tableExists = $this->hasTable($table);
81 120
        if ($tableExists) {
82 120
            $this->drop($table);
83
        }
84 120
    }
85
86
    /**
87
     * Get all the tables for the database; excluding ArangoDB system collections
88
     *
89
     * @return array<mixed>
90
     * @throws ArangoException
91
     */
92 1
    public function getAllTables(): array
93
    {
94 1
        return $this->schemaManager->getCollections(true);
95
    }
96
97
    /**
98
     * Rename a table (collection).
99
     *
100
     * @param $from
101
     * @param $to
102
     *
103
     * @return bool
104
     * @throws ArangoException
105
     *
106
     */
107 1
    public function rename($from, $to)
108
    {
109 1
        return (bool) $this->schemaManager->renameCollection($from, $to);
110
    }
111
112
    /**
113
     * Drop a table (collection) from the schema.
114
     *
115
     * @throws ArangoException
116
     */
117 120
    public function drop($table)
118
    {
119 120
        $this->schemaManager->deleteCollection($table);
120 120
    }
121
122
    /**
123
     * Drop all tables (collections) from the schema.
124
     *
125
     * @throws ArangoException
126
     */
127 1
    public function dropAllTables(): void
128
    {
129 1
        $collections = $this->getAllTables();
130
131 1
        foreach ($collections as $name) {
132 1
            $this->schemaManager->deleteCollection($name->name);
133
        }
134 1
    }
135
136
    /**
137
     * Determine if the given table has a given column.
138
     *
139
     * @param  string  $table
140
     * @param  string  $column
141
     * @return bool
142
     */
143 1
    public function hasColumn($table, $column)
144
    {
145 1
        if (is_string($column)) {
0 ignored issues
show
introduced by
The condition is_string($column) is always true.
Loading history...
146 1
            $column = [$column];
147
        }
148
149 1
        return $this->hasColumns($table, $column);
150
    }
151
152
    /**
153
     * Determine if the given table has given columns.
154
     *
155
     * @param  string  $table
156
     * @param  array  $columns
157
     * @return bool
158
     */
159 1
    public function hasColumns($table, array $columns)
160
    {
161 1
        $parameters = [];
162 1
        $parameters['name'] = 'hasAttribute';
163 1
        $parameters['handler'] = 'aql';
164 1
        $parameters['attribute'] = $columns;
165
166 1
        $command = new Fluent($parameters);
167 1
        $compilation = $this->grammar->compileHasAttribute($table, $command);
168
169 1
        return $this->connection->statement($compilation['aql']);
170
    }
171
172
    /**
173
     * @param  array<mixed> $properties
174
     * @throws ArangoException
175
     */
176 7
    public function createView(string $name, array $properties, string $type = 'arangosearch')
177
    {
178 7
        $view = $properties;
179 7
        $view['name'] = $name;
180 7
        $view['type'] = $type;
181
182 7
        $this->schemaManager->createView($view);
183 7
    }
184
185
    /**
186
     * @param  string  $name
187
     *
188
     * @return mixed
189
     * @throws ArangoException
190
     */
191 1
    public function getView(string $name): stdClass
192
    {
193 1
        return $this->schemaManager->getView($name);
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
     * Drop all views from the schema.
236
     *
237
     * @throws ArangoException
238
     */
239 1
    public function dropAllViews(): void
240
    {
241 1
        $views = $this->schemaManager->getViews();
242
243 1
        foreach ($views as $view) {
244 1
            $this->schemaManager->deleteView($view->name);
245
        }
246 1
    }
247
248
    /**
249
     * Create a database in the schema.
250
     *
251
     * @param string $name
252
     * @return bool
253
     *
254
     * @throws ArangoException
255
     */
256 2
    public function createDatabase($name)
257
    {
258 2
        return $this->schemaManager->createDatabase($name);
259
    }
260
261
    /**
262
     * Drop a database from the schema if the database exists.
263
     *
264
     * @param string $name
265
     * @return bool
266
     *
267
     * @throws ArangoException
268
     */
269 2
    public function dropDatabaseIfExists($name)
270
    {
271 2
        if ($this->schemaManager->hasDatabase($name)) {
272 1
            return $this->schemaManager->deleteDatabase($name);
273
        }
274
275 1
        return true;
276
    }
277
278
279
    /**
280
     * Get the database connection instance.
281
     *
282
     * @return Connection
283
     */
284 1
    public function getConnection()
285
    {
286 1
        return $this->connection;
287
    }
288
289 125
    protected function handleExceptionsAsQueryExceptions(Closure $callback)
290
    {
291
        try {
292 125
            return $callback();
293 2
        } catch (\Exception $e) {
294 2
            throw new QueryException($e->getMessage(), [], $e);
295
        }
296
    }
297
298
    /**
299
     * Silently catch the use of unsupported builder methods.
300
     */
301 1
    public function __call(string $method, mixed $args): void
302
    {
303 1
        Log::warning("The Aranguent Schema Builder doesn't support method '$method'\n");
304 1
    }
305
}
306