Completed
Push — master ( 533b47...6c16e6 )
by Gabriel
02:01
created

Connection   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 21.31%

Importance

Changes 0
Metric Value
wmc 24
lcom 3
cbo 6
dl 0
loc 253
ccs 13
cts 61
cp 0.2131
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A connect() 0 13 3
A getDatabase() 0 4 1
A setDatabase() 0 4 1
A getMetadata() 0 9 2
A tableName() 0 4 1
A newSelect() 0 4 1
A newQuery() 0 9 1
A newInsert() 0 4 1
A newUpdate() 0 4 1
A newDelete() 0 4 1
A execute() 0 12 2
A lastInsertID() 0 4 1
A affectedRows() 0 4 1
A disconnect() 0 10 3
A describeTable() 0 4 1
A protect() 0 4 1
A getQueries() 0 4 1
1
<?php
2
3
namespace Nip\Database\Connections;
4
5
use Exception;
6
use Nip\Database\Adapters\HasAdapterTrait;
7
use Nip\Database\Metadata\Manager as MetadataManager;
8
use Nip\Database\Query\AbstractQuery as AbstractQuery;
9
use Nip\Database\Query\Delete as DeleteQuery;
10
use Nip\Database\Query\Insert as InsertQuery;
11
use Nip\Database\Query\Select as SelectQuery;
12
use Nip\Database\Query\Update as UpdateQuery;
13
use Nip\Database\Result;
14
15
/**
16
 * Class Connection
17
 * @package Nip\Database
18
 */
19
class Connection
20
{
21
    use HasAdapterTrait;
22
23
    /**
24
     * The active PDO connection.
25
     *
26
     * @var PDO
27
     */
28
    protected $pdo;
29
30
    /**
31
     * The name of the connected database.
32
     *
33
     * @var string
34
     */
35
    protected $database;
36
    /**
37
     * The table prefix for the connection.
38
     *
39
     * @var string
40
     */
41
    protected $tablePrefix = '';
42
43
    /**
44
     * The database connection configuration options.
45
     *
46
     * @var array
47
     */
48
    protected $config = [];
49
50
    protected $metadata;
51
52
    protected $_query;
53
54
    protected $_queries = [];
55
56
    /**
57
     * Create a new database connection instance.
58
     *
59
     * @param  \PDO|\Closure $pdo
60
     * @param  string $database
61
     * @param  string $tablePrefix
62
     * @param  array $config
63
     */
64 22
    public function __construct($pdo, $database = '', $tablePrefix = '', $config = [])
65
    {
66 22
        $this->pdo = $pdo;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo of type object<PDO> or object<Closure> is incompatible with the declared type object<Nip\Database\Connections\PDO> of property $pdo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
67
68
        // First we will setup the default properties. We keep track of the DB
69
        // name we are connected to since it is needed when some reflective
70
        // type commands are run such as checking whether a table exists.
71 22
        $this->database = $database;
72
73 22
        $this->tablePrefix = $tablePrefix;
74 22
        $this->config = $config;
75
76
        // We need to initialize a query grammar and the query post processors
77
        // which are both very important parts of the database abstractions
78
        // so we initialize these to their default values while starting.
79
//        $this->useDefaultQueryGrammar();
80
//        $this->useDefaultPostProcessor();
81 22
    }
82
83
    /**
84
     * Connects to SQL server
85
     *
86
     * @param string $host
87
     * @param string $user
88
     * @param string $password
89
     * @param string $database
90
     * @param bool $newLink
91
     * @return static
92
     */
93
    public function connect($host, $user, $password, $database, $newLink = false)
94
    {
95
        if (!$this->pdo) {
96
            try {
97
                $this->pdo = $this->getAdapter()->connect($host, $user, $password, $database, $newLink);
0 ignored issues
show
Documentation introduced by
$host is of type string, but the function expects a boolean.

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...
Documentation introduced by
$user is of type string, but the function expects a boolean.

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...
Documentation introduced by
$password is of type string, but the function expects a boolean.

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...
Documentation introduced by
$database is of type string, but the function expects a boolean.

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...
98
                $this->setDatabase($database);
99
            } catch (Exception $e) {
100
                $e->log();
101
            }
102
        }
103
104
        return $this;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getDatabase()
111
    {
112
        return $this->database;
113
    }
114
115
    /**
116
     * @param string $database
117
     */
118
    public function setDatabase($database)
119
    {
120
        $this->database = $database;
121
    }
122
123
    /**
124
     * @return MetadataManager
125
     */
126
    public function getMetadata()
127
    {
128
        if (!$this->metadata) {
129
            $this->metadata = new MetadataManager();
130
            $this->metadata->setConnection($this);
131
        }
132
133
        return $this->metadata;
134
    }
135
136
    /**
137
     * Prefixes table names
138
     *
139
     * @param string $table
140
     * @return string
141
     */
142
    public function tableName($table)
143
    {
144
        return $table;
145
    }
146
147
    /**
148
     * @return AbstractQuery|SelectQuery
149
     */
150
    public function newSelect()
151
    {
152
        return $this->newQuery('select');
153
    }
154
155
    /**
156
     * @param string $type optional
157
     * @return AbstractQuery|SelectQuery|UpdateQuery|InsertQuery|DeleteQuery
158
     */
159 12
    public function newQuery($type = "select")
160
    {
161 12
        $className = '\Nip\Database\Query\\' . inflector()->camelize($type);
162 12
        $query = new $className();
163
        /** @var AbstractQuery $query */
164 12
        $query->setManager($this);
165
166 12
        return $query;
167
    }
168
169
    /**
170
     * @return InsertQuery
171
     */
172
    public function newInsert()
173
    {
174
        return $this->newQuery('insert');
175
    }
176
177
    /**
178
     * @return UpdateQuery
179
     */
180 1
    public function newUpdate()
181
    {
182 1
        return $this->newQuery('update');
183
    }
184
185
    /**
186
     * @return DeleteQuery
187
     */
188
    public function newDelete()
189
    {
190
        return $this->newQuery('delete');
191
    }
192
193
    /**
194
     * Executes SQL query
195
     *
196
     * @param mixed|AbstractQuery $query
197
     * @return Result
198
     */
199
    public function execute($query)
200
    {
201
        $this->_queries[] = $query;
202
203
        $sql = is_string($query) ? $query : $query->getString();
204
205
        $resultSQL = $this->getAdapter()->execute($sql);
206
        $result = new Result($resultSQL, $this->getAdapter());
207
        $result->setQuery($query);
208
209
        return $result;
210
    }
211
212
    /**
213
     * Gets the ID of the last inserted record
214
     * @return int
215
     */
216
    public function lastInsertID()
217
    {
218
        return $this->getAdapter()->lastInsertID();
219
    }
220
221
    /**
222
     * Gets the number of rows affected by the last operation
223
     * @return int
224
     */
225
    public function affectedRows()
226
    {
227
        return $this->getAdapter()->affectedRows();
228
    }
229
230
    /**
231
     * Disconnects from server
232
     */
233
    public function disconnect()
234
    {
235
        if ($this->pdo) {
236
            try {
237
                $this->getAdapter()->disconnect();
238
            } catch (Exception $e) {
239
                $e->log();
240
            }
241
        }
242
    }
243
244
    /**
245
     * @param null|string $table
246
     * @return mixed
247
     */
248
    public function describeTable($table)
249
    {
250
        return $this->getAdapter()->describeTable($this->protect($table));
251
    }
252
253
    /**
254
     * Adds backticks to input
255
     *
256
     * @param string $input
257
     * @return string
258
     */
259
    public function protect($input)
260
    {
261
        return str_replace("`*`", "*", '`' . str_replace('.', '`.`', $input) . '`');
262
    }
263
264
    /**
265
     * @return array
266
     */
267
    public function getQueries()
268
    {
269
        return $this->_queries;
270
    }
271
}
272