Test Setup Failed
Push — testing ( e1d3e5...3b0ebf )
by Roman
02:43
created

Connection   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 362
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 5
dl 0
loc 362
rs 10
c 0
b 0
f 0
ccs 90
cts 90
cp 1

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A getDatabase() 0 10 2
A table() 0 8 1
A getCassandraCluster() 0 4 1
A getCassandraSession() 0 4 1
A getKeyspace() 0 4 1
A disconnect() 0 5 1
A getDriverName() 0 4 1
A select() 0 4 1
A insertBulk() 0 4 1
A batchStatement() 0 17 3
A statement() 0 4 1
A affectingStatement() 0 4 1
A getDefaultPostProcessor() 0 4 1
A getDefaultQueryGrammar() 0 4 1
A getDefaultSchemaGrammar() 0 4 1
A reconnectIfMissingConnection() 0 6 2
A __call() 0 4 1
A runStatement() 0 17 3
A transaction() 0 4 1
A beginTransaction() 0 4 1
A commit() 0 4 1
A rollBack() 0 4 1
A transactionLevel() 0 4 1
1
<?php
2
3
namespace lroman242\LaravelCassandra;
4
5
use Cassandra;
6
use Cassandra\BatchStatement;
7
use \lroman242\LaravelCassandra\Exceptions\CassandraNotSupportedException;
8
9
class Connection extends \Illuminate\Database\Connection
10
{
11
    /**
12
     * The Cassandra keyspace
13
     *
14
     * @var string
15
     */
16
    protected $keyspace;
17
18
    /**
19
     * The Cassandra connection handler.
20
     *
21
     * @var \Cassandra\Session
22
     */
23
    protected $session;
24
25
    /**
26
     * The config
27
     *
28
     * @var array
29
     */
30
    protected $config;
31
32
    /**
33
     * The Cassandra cluster
34
     *
35
     * @var \Cassandra\Cluster
36
     */
37
    protected $cluster;
38
39
    /**
40
     * Connection constructor.
41
     * @param Cassandra\Cluster $cluster
42
     * @param array $config
43
     */
44
    public function __construct(\Cassandra\Cluster $cluster, array $config)
45
    {
46
        $this->config = $config;
47 48
48
        $this->cluster = $cluster;
49 48
50 48
        $this->keyspace = $this->getDatabase($config);
51 48
52
        $this->session = $this->cluster->connect($this->keyspace);
53
54
        $this->useDefaultPostProcessor();
55 48
56
        $this->useDefaultSchemaGrammar();
57 48
58 48
        $this->setQueryGrammar($this->getDefaultQueryGrammar());
59
    }
60 48
61 48
    /**
62
     * Get keyspace name from config
63
     *
64 48
     * @param array $config
65
     *
66 48
     * @return string|null
67
     */
68 48
    protected function getDatabase(array $config)
69 48
    {
70
        $keyspaceName = null;
71
72
        if (isset($config['keyspace'])) {
73
            $keyspaceName = $config['keyspace'];
74
        }
75
76
        return $keyspaceName;
77 5
    }
78
79 5
    /**
80
     * Begin a fluent query against a database table.
81 5
     *
82
     * @param string $table
83 5
     * @return Query\Builder
84
     */
85
    public function table($table)
86
    {
87
        $processor = $this->getPostProcessor();
88
89
        $query = new Query\Builder($this, null, $processor);
0 ignored issues
show
Documentation introduced by
$processor is of type object<Illuminate\Databa...y\Processors\Processor>, but the function expects a null|object<lroman242\La...sandra\Query\Processor>.

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...
90
91 1
        return $query->from($table);
92
    }
93 1
94
    /**
95
     * return Cassandra cluster.
96
     *
97
     * @return \Cassandra\Cluster
98
     */
99
    public function getCassandraCluster()
100
    {
101 3
        return $this->cluster;
102
    }
103 3
104
    /**
105
     * return Cassandra Session.
106
     *
107
     * @return \Cassandra\Session
108
     */
109
    public function getCassandraSession()
110
    {
111 1
        return $this->session;
112
    }
113 1
114
    /**
115
     * Return the Cassandra keyspace
116
     *
117
     * @return string
118
     */
119
    public function getKeyspace()
120
    {
121
        return $this->keyspace;
122
    }
123 48
124
    /**
125
     * Disconnect from the underlying Cassandra connection.
126 48
     */
127 48
    public function disconnect()
128
    {
129
        $this->session->close();
130 48
        $this->session = null;
131 48
    }
132
133 48
    /**
134 48
     * Get the PDO driver name.
135
     *
136
     * @return string
137 48
     */
138
    public function getDriverName()
139
    {
140 48
        return 'cassandra';
141 48
    }
142
143 48
    /**
144
     * Run a select statement against the database.
145
     *
146
     * @param  string  $query
147
     * @param  array  $bindings
148
     * @param  bool  $useReadPdo
149
     * @param  array  $customOptions
150
     *
151
     * @return mixed
152 48
     */
153
    public function select($query, $bindings = [], $useReadPdo = true, array $customOptions = [])
154 48
    {
155 48
        return $this->statement($query, $bindings, $customOptions);
156
    }
157
158
    /**
159
     * Run an bulk insert statement against the database.
160
     *
161 48
     * @param  array  $queries
162
     * @param  array  $bindings
163
     * @param  int  $type
164 48
     * @param  array  $customOptions
165 48
     *
166
     * @return bool
167
     */
168 48
    public function insertBulk($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = [])
169 48
    {
170
        return $this->batchStatement($queries, $bindings, $type, $customOptions);
171
    }
172 48
173 48
    /**
174
     * Execute a group of queries inside a batch statement against the database.
175
     *
176 48
     * @param  array  $queries
177 48
     * @param  array  $bindings
178
     * @param  int  $type
179
     * @param  array  $customOptions
180
     *
181
     * @return bool
182
     */
183
    public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = [])
184 48
    {
185
        return $this->run($queries, $bindings, function ($queries, $bindings) use ($type, $customOptions) {
0 ignored issues
show
Documentation introduced by
$queries is of type array, but the function expects a string.

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...
186 48
            if ($this->pretending()) {
187
                return [];
188
            }
189 48
190
            $batch = new BatchStatement($type);
191 48
192
            foreach ($queries as $k => $query) {
193 48
                $preparedStatement = $this->session->prepare($query);
194
                $batch->add($preparedStatement, $bindings[$k]);
195
            }
196
197
            return $this->session->execute($batch, $customOptions);
198
        });
199 3
    }
200
201 3
    /**
202 3
     * Execute an CQL statement and return the boolean result.
203 3
     *
204
     * @param  string  $query
205
     * @param  array   $bindings
206
     * @param  array  $customOptions
207
     *
208
     * @return mixed
209
     */
210 1
    public function statement($query, $bindings = [], array $customOptions = [])
211
    {
212 1
        return $this->runStatement($query, $bindings, $customOptions);
213
    }
214
215
    /**
216
     * Because Cassandra is an eventually consistent database, it's not possible to obtain
217
     * the affected count for statements so we're just going to return 0, based on the idea
218
     * that if the query fails somehow, an exception will be thrown
219
     *
220
     * @param  string  $query
221
     * @param  array   $bindings
222
     * @param  array  $customOptions
223
     *
224
     * @return int
225 48
     */
226
    public function affectingStatement($query, $bindings = [], array $customOptions = [])
227 48
    {
228
        return $this->runStatement($query, $bindings, $customOptions, 0, 1);
229
    }
230
231
    /**
232
     * @inheritdoc
233
     */
234
    protected function getDefaultPostProcessor()
235
    {
236
        return new Query\Processor();
237
    }
238
239
    /**
240 6
     * @inheritdoc
241
     */
242 6
    protected function getDefaultQueryGrammar()
243
    {
244
        return new Query\Grammar();
245
    }
246
247
    /**
248
     * @inheritdoc
249
     */
250
    protected function getDefaultSchemaGrammar()
251
    {
252
        //return new Schema\Grammar();
253
    }
254
255
    /**
256
     * Reconnect to the database if connection is missing.
257 7
     *
258 7
     * @return void
259 1
     */
260
    protected function reconnectIfMissingConnection()
261
    {
262 6
        if (is_null($this->session)) {
263
            $this->session = $this->cluster->connect($this->keyspace);
264 6
        }
265 6
    }
266 6
267
    /**
268
     * Dynamically pass methods to the connection.
269 6
     *
270 7
     * @param  string  $method
271
     * @param  array   $parameters
272
     * @return mixed
273
     */
274
    public function __call($method, $parameters)
275
    {
276
        return call_user_func_array([$this->session, $method], $parameters);
277
    }
278
279
    /**
280
     * Execute an CQL statement and return the boolean result.
281
     *
282 48
     * @param string $query
283
     * @param array $bindings
284 48
     * @param array $customOptions
285
     * @param mixed $defaultFailed
286
     * @param mixed $defaultSuccess
287
     *
288
     * @return mixed
289
     */
290
    protected function runStatement($query, $bindings = [], array $customOptions = [], $defaultFailed = [], $defaultSuccess = null)
291
    {
292
        return $this->run($query, $bindings, function ($query, $bindings) use ($customOptions, $defaultFailed, $defaultSuccess) {
293
            if ($this->pretending()) {
294
                return $defaultFailed;
295
            }
296
297
            $preparedStatement = $this->session->prepare($query);
298 10
299
            //Add bindings
300 10
            $customOptions['arguments'] = $bindings;
301
302
            $result = $this->session->execute($preparedStatement, $customOptions);
303
304
            return $defaultSuccess === null ? $result : $defaultSuccess;
305
        });
306 48
    }
307
308 48
    /**
309
     * @inheritDoc
310
     */
311
    public function transaction(\Closure $callback, $attempts = 1)
312
    {
313
        throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database");
314 48
    }
315
316 48
    /**
317
     * @inheritDoc
318
     */
319
    public function beginTransaction()
320
    {
321
        throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database");
322 48
    }
323
324
    /**
325 48
     * @inheritDoc
326
     */
327
    public function commit()
328
    {
329
        throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database");
330
    }
331
332 48
    /**
333
     * @inheritDoc
334 48
     */
335 1
    public function rollBack()
336
    {
337 48
        throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database");
338
    }
339
340
    /**
341
     * @inheritDoc
342
     */
343
    public function transactionLevel()
344
    {
345
        throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database");
346 1
    }
347
348 1
    //TODO: override isDoctrineAvailable method
349
    //TODO: override getDoctrineColumn method
350
    //TODO: override getDoctrineSchemaManager method
351
    //TODO: override getDoctrineConnection method
352
    //TODO: override getPdo method
353
    //TODO: override getReadPdo method
354
    //TODO: override setPdo method
355
    //TODO: override setReadPdo method
356
    //TODO: override setReconnector method
357
    //TODO: override reconnect method
358
    //TODO: override query method
359
360
    //TODO: override bindValues method
361
    //TODO: override cursor method
362
    //TODO: override unprepared method
363
364 48
    //TODO: check prepareBindings method
365 48
366 2
    //TODO: provide interface for $this->session->executeAsync
367
    //TODO: provide interface for $this->session->prepareAsync
368
    //TODO: provide interface for $this->session->closeAsync
369 48
    //TODO: provide interface for $this->session->schema
370
}
371