Completed
Push — master ( ec22e7...ed71fb )
by Terry
04:05
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
     * @var CacheInterface
41
     */
42
    protected $_cache = null;
43
    /**
44
     * @var bool
45
     */
46
    protected $_transactionCount = 0;
47
48
    /**
49
     * @param AbstractLogger $logger
50
     *
51
     * @return AbstractPdo
52
     */
53
    public function setLogger(AbstractLogger $logger)
54
    {
55
        $this->_logger = $logger;
56
        return $this;
57
    }
58
59
    /**
60
     * @return AbstractLogger
61
     */
62
    public function getLogger()
63
    {
64
        return $this->_logger;
65
    }
66
67
    /**
68
     * @return CacheInterface
69
     */
70
    public function getCache()
71
    {
72
        return $this->_cache;
73
    }
74
75
    /**
76
     * @param array  $config
77
     * @param string $dsn
78
     * @return $this
79
     */
80
    public function setConfig(array $config = [], $dsn='')
81
    {
82
        if ( $dsn )
83
        {
84
            $driver = ucfirst(strtolower(substr($dsn, 0, strpos($dsn, ':'))));
85
            $dsn    = preg_replace("/^{$driver}:/i", '', $dsn);
86
            $parts  = explode(';', $dsn);
87
            foreach ( $parts as $conf )
88
            {
89
                $conf = explode('=', $conf);
90
                $config[$conf[0]] = isset($conf[1]) ? $conf[1] : true;
91
            }
92
            $this->_config['driver'] = $driver;
93
        }
94
        $this->_config = $config;
95
        return $this;
96
    }
97
98
    /**
99
     * @param $key
100
     * @return mixed
101
     */
102
    public function getConfig($key)
103
    {
104
        return isset($this->_config[$key]) ? $this->_config[$key] : null;
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function getAllConfig()
111
    {
112
        return $this->_config;
113
    }
114
115
116
    /**
117
     * @return bool
118
     */
119
    public function logQueries()
120
    {
121
        return !empty( $this->_config['log_queries'] ) && $this->_config['log_queries'] ? true : false;
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function getTransactionDepth()
128
    {
129
        return $this->_transactionCount;
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function beginTransaction()
136
    {
137
        $this->_transactionCount++;
138
        if ( $this->_transactionCount === 1 )
139
        {
140
            return parent::beginTransaction();
141
        }
142
        return true;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function commit()
149
    {
150
        $this->_transactionCount--;
151
        $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...
152
        if ( $this->_transactionCount === 0 )
153
        {
154
            return parent::commit();
155
        }
156
        return true;
157
    }
158
159
    /**
160
     * @return bool
161
     */
162
    public function rollback()
163
    {
164
        if ( $this->_transactionCount > 0 )
165
        {
166
            $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...
167
            return parent::rollBack();
168
        }
169
        return true;
170
    }
171
172
    /**
173
     * @param string $query
174
     * @param integer $limit
175
     * @param null|integer $offset
176
     * @return string
177
     */
178
    public function setLimit($query, $limit=null, $offset=null)
179
    {
180
        Assert($query)->string()->notEmpty();
181
        Assert($limit)->nullOr()->integer();
182
        Assert($offset)->nullOr()->integer();
183
        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...
184
        {
185
            $query .= " LIMIT {$limit}";
186
        }
187
        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...
188
        {
189
            $query .= " OFFSET {$offset}";
190
        }
191
        return $query;
192
    }
193
194
    /**
195
     * @param bool $include_views
196
     * @return array
197
     */
198
    abstract public function getTables($include_views=false);
199
200
    /**
201
     * @param bool $include_views
202
     * @param null $table
203
     * @return array|null
204
     */
205
    abstract public function getColumns($include_views=false, $table=null);
206
207
    /**
208
     * @param null $table
209
     * @return array
210
     */
211
    abstract public function getForeignKeys($table = null);
212
213
    /**
214
     * @param bool|false $include_views
215
     * @param null|string $table
216
     * @return array
217
     */
218
    abstract public function getTableCounts($include_views=false, $table=null);
219
220
}