Completed
Push — master ( ed71fb...26b715 )
by Terry
02:36
created

AbstractPdo::getCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * @package Terah\FluentPdoModel
4
 *
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
10
 */
11
12
namespace Terah\FluentPdoModel\Drivers;
13
14
use Psr\Log\AbstractLogger;
15
use Terah\RedisCache\CacheInterface;
16
use \PDO;
17
use function Terah\Assert\Assert;
18
use function Terah\Assert\Validate;
19
use Terah\RedisCache\RedisCacheTrait;
20
21
/**
22
 * Class AbstractPdo
23
 *
24
 * @package Terah\FluentPdoModel\Drivers
25
 * @author  Terry Cullen - [email protected]
26
 */
27
abstract class AbstractPdo extends PDO implements DriverInterface
28
{
29
    use RedisCacheTrait;
30
31
    /**
32
     * @var array
33
     */
34
    protected $_config = [];
35
    /**
36
     * @var AbstractLogger
37
     */
38
    protected $_logger = null;
39
40
    /**
41
     * @var bool
42
     */
43
    protected $_transactionCount = 0;
44
45
    /**
46
     * @param AbstractLogger $logger
47
     *
48
     * @return AbstractPdo
49
     */
50
    public function setLogger(AbstractLogger $logger)
51
    {
52
        $this->_logger = $logger;
53
        return $this;
54
    }
55
56
    /**
57
     * @return AbstractLogger
58
     */
59
    public function getLogger()
60
    {
61
        return $this->_logger;
62
    }
63
64
    /**
65
     * @param array  $config
66
     * @param string $dsn
67
     * @return $this
68
     */
69
    public function setConfig(array $config = [], $dsn='')
70
    {
71
        if ( $dsn )
72
        {
73
            $driver = ucfirst(strtolower(substr($dsn, 0, strpos($dsn, ':'))));
74
            $dsn    = preg_replace("/^{$driver}:/i", '', $dsn);
75
            $parts  = explode(';', $dsn);
76
            foreach ( $parts as $conf )
77
            {
78
                $conf = explode('=', $conf);
79
                $config[$conf[0]] = isset($conf[1]) ? $conf[1] : true;
80
            }
81
            $this->_config['driver'] = $driver;
82
        }
83
        $this->_config = $config;
84
        return $this;
85
    }
86
87
    /**
88
     * @param $key
89
     * @return mixed
90
     */
91
    public function getConfig($key)
92
    {
93
        return isset($this->_config[$key]) ? $this->_config[$key] : null;
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function getAllConfig()
100
    {
101
        return $this->_config;
102
    }
103
104
105
    /**
106
     * @return bool
107
     */
108
    public function logQueries()
109
    {
110
        return !empty( $this->_config['log_queries'] ) && $this->_config['log_queries'] ? true : false;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116
    public function getTransactionDepth()
117
    {
118
        return $this->_transactionCount;
119
    }
120
121
    /**
122
     * @return bool
123
     */
124
    public function beginTransaction()
125
    {
126
        $this->_transactionCount++;
127
        if ( $this->_transactionCount === 1 )
128
        {
129
            return parent::beginTransaction();
130
        }
131
        return true;
132
    }
133
134
    /**
135
     * @return bool
136
     */
137
    public function commit()
138
    {
139
        $this->_transactionCount--;
140
        $this->_transactionCount = $this->_transactionCount < 0 ? 0 : $this->_transactionCount;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_transactionCount...this->_transactionCount can also be of type integer. However, the property $_transactionCount is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
141
        if ( $this->_transactionCount === 0 )
142
        {
143
            return parent::commit();
144
        }
145
        return true;
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    public function rollback()
152
    {
153
        if ( $this->_transactionCount > 0 )
154
        {
155
            $this->_transactionCount = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $_transactionCount was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
156
            return parent::rollBack();
157
        }
158
        return true;
159
    }
160
161
    /**
162
     * @param string $query
163
     * @param integer $limit
164
     * @param null|integer $offset
165
     * @return string
166
     */
167
    public function setLimit($query, $limit=null, $offset=null)
168
    {
169
        Assert($query)->string()->notEmpty();
170
        Assert($limit)->nullOr()->integer();
171
        Assert($offset)->nullOr()->integer();
172
        if ( $limit )
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
173
        {
174
            $query .= " LIMIT {$limit}";
175
        }
176
        if ( $offset )
0 ignored issues
show
Bug Best Practice introduced by
The expression $offset of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
177
        {
178
            $query .= " OFFSET {$offset}";
179
        }
180
        return $query;
181
    }
182
183
    /**
184
     * @param bool $include_views
185
     * @return array
186
     */
187
    abstract public function getTables($include_views=false);
188
189
    /**
190
     * @param bool $include_views
191
     * @param null $table
192
     * @return array|null
193
     */
194
    abstract public function getColumns($include_views=false, $table=null);
195
196
    /**
197
     * @param null $table
198
     * @return array
199
     */
200
    abstract public function getForeignKeys($table = null);
201
202
    /**
203
     * @param bool|false $include_views
204
     * @param null|string $table
205
     * @return array
206
     */
207
    abstract public function getTableCounts($include_views=false, $table=null);
208
209
}