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

Connection::getSchemaBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 3
b 1
f 0
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent;
4
5
use ArangoClient\ArangoClient;
6
use Illuminate\Database\Connection as IlluminateConnection;
7
use LaravelFreelancerNL\Aranguent\Concerns\DetectsDeadlocks;
8
use LaravelFreelancerNL\Aranguent\Concerns\DetectsLostConnections;
9
use LaravelFreelancerNL\Aranguent\Concerns\HandlesArangoDb;
10
use LaravelFreelancerNL\Aranguent\Concerns\ManagesTransactions;
11
use LaravelFreelancerNL\Aranguent\Concerns\RunsQueries;
12
use LaravelFreelancerNL\Aranguent\Query\Grammar as QueryGrammar;
13
use LaravelFreelancerNL\Aranguent\Query\Processor;
14
use LaravelFreelancerNL\Aranguent\Schema\Builder as SchemaBuilder;
15
use LaravelFreelancerNL\FluentAQL\QueryBuilder as ArangoQueryBuilder;
16
use LogicException;
17
use Spatie\DataTransferObject\Exceptions\UnknownProperties;
18
19
class Connection extends IlluminateConnection
20
{
21
    use HandlesArangoDb;
22
    use DetectsDeadlocks;
23
    use DetectsLostConnections;
24
    use ManagesTransactions;
25
    use RunsQueries;
26
27
    protected ?ArangoClient $arangoClient = null;
28
29
    protected $database;
30
31
    /**
32
     * The ArangoDB driver name.
33
     *
34
     * @var string
35
     */
36
    protected string $driverName = 'arangodb';
37
38
    /**
39
     * Connection constructor.
40
     *
41
     * @param array $config
42
     * @throws UnknownProperties
43
     */
44 199
    public function __construct($config = [])
45
    {
46 199
        $this->config = $config;
47
48 199
        $this->database = (isset($this->config['database'])) ? $this->config['database'] : null;
49 199
        $this->tablePrefix = $this->config['tablePrefix'] ?? null;
50
51
        // activate and set the database client connection
52 199
        $this->arangoClient = new ArangoClient($this->config);
53
54
        // We need to initialize a query grammar and the query post processors
55
        // which are both very important parts of the database abstractions
56
        // so, we initialize these to their default values while starting.
57 199
        $this->useDefaultQueryGrammar();
58
59 199
        $this->useDefaultPostProcessor();
60 199
    }
61
62
    /**
63
     * Get a schema builder instance for the connection.
64
     *
65
     * @return SchemaBuilder
66
     */
67 146
    public function getSchemaBuilder(): SchemaBuilder
68
    {
69 146
        if (is_null($this->schemaGrammar)) {
70
            $this->useDefaultSchemaGrammar();
71
        }
72
73 146
        return new SchemaBuilder($this);
74
    }
75
76
    /**
77
     * Get the default query grammar instance.
78
     *
79
     * @return QueryGrammar
80
     */
81 199
    protected function getDefaultQueryGrammar(): QueryGrammar
82
    {
83 199
        return new QueryGrammar();
84
    }
85
86
    /**
87
     * Get the default post processor instance.
88
     *
89
     * @return Processor
90
     */
91 199
    protected function getDefaultPostProcessor(): Processor
92
    {
93 199
        return new Processor();
94
    }
95
96
    /**
97
     * Get the collection prefix for the connection.
98
     *
99
     * @return string
100
     */
101
    public function getTablePrefix(): string
102
    {
103
        return $this->tablePrefix;
104
    }
105
106
    /**
107
     * Disconnect from the underlying ArangoDB connection.
108
     *
109
     * @return void
110
     */
111 7
    public function disconnect()
112
    {
113 7
        $this->arangoClient = null;
114 7
    }
115
116
    /**
117
     * Reconnect to the database.
118
     *
119
     * @return void
120
     *
121
     * @throws \LogicException
122
     */
123 3
    public function reconnect()
124
    {
125 3
        if (is_callable($this->reconnector)) {
126 3
            $this->arangoClient = null;
127
128 3
            $result = call_user_func($this->reconnector, $this);
129
130 3
            return $result;
131
        }
132
133
        throw new LogicException('Lost connection and no reconnector available.');
134
    }
135
136
    /**
137
     * Reconnect to the database if an ArangoDB connection is missing.
138
     *
139
     * @return void
140
     */
141 157
    protected function reconnectIfMissingConnection()
142
    {
143 157
        if (is_null($this->arangoClient)) {
144
            $this->reconnect();
145
        }
146 157
    }
147
148 158
    public function getArangoClient(): ArangoClient|null
149
    {
150 158
        return $this->arangoClient;
151
    }
152
153
    /**
154
     * @param  string|null  $database
155
     */
156 1
    public function setDatabaseName($database): void
157
    {
158 1
        $this->database = $database;
159 1
        $this->arangoClient->setDatabase($database);
0 ignored issues
show
Bug introduced by
It seems like $database can also be of type null; however, parameter $name of ArangoClient\ArangoClient::setDatabase() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
        $this->arangoClient->setDatabase(/** @scrutinizer ignore-type */ $database);
Loading history...
Bug introduced by
The method setDatabase() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
        $this->arangoClient->/** @scrutinizer ignore-call */ 
160
                             setDatabase($database);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
160 1
    }
161
162 2
    public function getDatabaseName(): string
163
    {
164 2
        return $this->database;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->database could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
165
    }
166
167
    /**
168
     * @param  ArangoQueryBuilder|string  $query
169
     * @param  array  $bindings
170
     * @return array
171
     */
172 158
    private function handleQueryBuilder($query, array $bindings): array
0 ignored issues
show
Unused Code introduced by
The method handleQueryBuilder() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
173
    {
174 158
        if ($query instanceof ArangoQueryBuilder) {
175 124
            $bindings = $query->binds;
176 124
            $query = $query->query;
177
        }
178 158
        return [$query, $bindings];
179
    }
180
181 4
    public static function aqb(): ArangoQueryBuilder
182
    {
183 4
        return new ArangoQueryBuilder();
184
    }
185
}
186