Completed
Push — devops/catch-breaking-changes-... ( 88c9a6 )
by Bas
28s queued 18s
created

Connection::getTablePrefix()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
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;
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...
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 207
    public function __construct($config = [])
45
    {
46 207
        $this->config = $config;
47
48 207
        $this->database = (isset($this->config['database'])) ? $this->config['database'] : null;
49 207
        $this->tablePrefix = $this->config['tablePrefix'] ?? null;
50
51
        // activate and set the database client connection
52 207
        $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 207
        $this->useDefaultQueryGrammar();
58
59 207
        $this->useDefaultPostProcessor();
60
    }
61
62
    /**
63
     * Get a schema builder instance for the connection.
64
     *
65
     * @return SchemaBuilder
66
     */
67 27
    public function getSchemaBuilder(): SchemaBuilder
68
    {
69 27
        if (is_null($this->schemaGrammar)) {
70
            $this->useDefaultSchemaGrammar();
71
        }
72
73 27
        return new SchemaBuilder($this);
74
    }
75
76
    /**
77
     * Get the default query grammar instance.
78
     *
79
     * @return QueryGrammar
80
     */
81 207
    protected function getDefaultQueryGrammar(): QueryGrammar
82
    {
83 207
        return new QueryGrammar();
84
    }
85
86
    /**
87
     * Get the default post processor instance.
88
     *
89
     * @return Processor
90
     */
91 207
    protected function getDefaultPostProcessor(): Processor
92
    {
93 207
        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 159
    public function disconnect()
112
    {
113 159
        $this->arangoClient = null;
114
    }
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
//            $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 175
    protected function reconnectIfMissingConnection()
142
    {
143 175
        if (is_null($this->arangoClient)) {
144
            $this->reconnect();
145
        }
146
    }
147
148 40
    public function getArangoClient(): ArangoClient|null
149
    {
150 40
        return $this->arangoClient;
151
    }
152
153
    /**
154
     * @param  string|null  $database
155
     */
156 2
    public function setDatabaseName($database): void
157
    {
158 2
        $this->database = $database;
159 2
        $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
    }
161
162 3
    public function getDatabaseName(): string
163
    {
164 3
        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 4
    public static function aqb(): ArangoQueryBuilder
168
    {
169 4
        return new ArangoQueryBuilder();
170
    }
171
}
172