Test Setup Failed
Push — testing ( 3b0ebf...4e021b )
by Roman
02:49
created

CassandraConnector::setDefaultQueryOptions()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 2
nop 1
1
<?php
2
3
4
namespace lroman242\LaravelCassandra;
5
6
7
use Illuminate\Database\Connectors\Connector;
8
use Illuminate\Queue\Connectors\ConnectorInterface;
9
10
class CassandraConnector extends Connector implements ConnectorInterface
11
{
12
    const DEFAULT_PAGE_SIZE = 5000;
13
14
    /**
15
     * Cassandra Cluster Builder instance
16
     *
17
     * @var \Cassandra\Cluster\Builder
18
     */
19
    protected $builder;
20
21
    /**
22
     * Establish a database connection.
23
     *
24
     * @param  array  $config
25
     *
26
     * @return \Cassandra\Cluster
27
     */
28
    public function connect(array $config)
29
    {
30
        $this->builder = \Cassandra::cluster();
31
32
        $this->setConnectionOptions($config);
33
34
        $this->setTimeouts($config);
35
36
        $this->setDefaultQueryOptions($config);
37
38
        return $this->builder->build();
39
    }
40
41
    /**
42
     * Set username and password to cluster connection.
43
     * Set cluster contact points (IP addresses)
44
     * Set connection communication port
45
     *
46
     * @param array $config
47
     */
48
    protected function setConnectionOptions(array $config)
49
    {
50
        // Authentication
51
        list($username, $password) = [
52
            $config['username'] ?? null, $config['password'] ?? null,
53
        ];
54
55
        $this->builder->withCredentials($username, $password);
56
57
        call_user_func_array([$this->builder, 'withContactPoints'], $this->getContactPoints($config));
58
59
        $this->builder->withPort($this->getPort($config));
60
    }
61
62
    /**
63
     * Parse contact points list from config
64
     *
65
     * @param array $config
66
     *
67
     * @return array
68
     */
69
    protected function getContactPoints(array $config)
70
    {
71
        $contactPoints = [];
72
73
        if (!empty($config['host'])) {
74
            $contactPoints = $config['host'];
75
76
            if (is_string($contactPoints)) {
77
                $contactPoints = explode(',', $contactPoints);
78
            }
79
        }
80
81
        return (array) $contactPoints;
82
    }
83
84
    /**
85
     * Parse communication port or set default
86
     *
87
     * @param array $config
88
     *
89
     * @return int
90
     */
91
    protected function getPort(array $config)
92
    {
93
        return !empty($config['port']) ? (int)$config['port'] : 9042;
94
    }
95
96
    /**
97
     * Set default consistency level
98
     * Set default response size to queries
99
     *
100
     * @param array $config
101
     */
102
    protected function setDefaultQueryOptions(array $config)
103
    {
104
        if (isset($config['consistency']) && in_array($config['consistency'], [
105
                \Cassandra::CONSISTENCY_ANY, \Cassandra::CONSISTENCY_ONE, \Cassandra::CONSISTENCY_TWO,
106
                \Cassandra::CONSISTENCY_THREE, \Cassandra::CONSISTENCY_QUORUM, \Cassandra::CONSISTENCY_ALL,
107
                \Cassandra::CONSISTENCY_SERIAL, \Cassandra::CONSISTENCY_QUORUM, \Cassandra::CONSISTENCY_LOCAL_QUORUM,
108
                \Cassandra::CONSISTENCY_EACH_QUORUM, \Cassandra::CONSISTENCY_LOCAL_SERIAL, \Cassandra::CONSISTENCY_LOCAL_ONE,
109
            ])) {
110
111
            $this->builder->withDefaultConsistency($config['consistency']);
112
        }
113
114
        $this->builder->withDefaultPageSize(intval(!empty($config['page_size']) ? $config['page_size'] : self::DEFAULT_PAGE_SIZE));
115
    }
116
117
    /**
118
     * Set timeouts for query execution
119
     * and cluster connection
120
     *
121
     * @param array $config
122
     */
123
    protected function setTimeouts(array $config)
124
    {
125
        if (!empty($config['timeout'])) {
126
            $this->builder->withDefaultTimeout(intval($config['timeout']));
127
        }
128
129
        if (!empty($config['connect_timeout'])) {
130
            $this->builder->withConnectTimeout(floatval($config['connect_timeout']));
131
        }
132
133
        if (!empty($config['request_timeout'])) {
134
            $this->builder->withRequestTimeout(floatval($config['request_timeout']));
135
        }
136
    }
137
}