Failed Conditions
Push — refactor/improve-static-analys... ( 8da3ef...a7b39f )
by Bas
09:53
created

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

172
        $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

172
        $this->arangoClient->/** @scrutinizer ignore-call */ 
173
                             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...
173
    }
174
175
    public function getDatabaseName(): string
176
    {
177
        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...
178
    }
179
180
    public static function aqb(): ArangoQueryBuilder
181
    {
182
        return new ArangoQueryBuilder();
183
    }
184
}
185