Passed
Push — master ( 074e22...c7a4af )
by Oleg
03:48
created

DbDriver::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 2
eloc 15
nc 2
nop 2
1
<?php /** MicroDbDriver */
2
3
namespace Micro\Cache\Driver;
4
5
use Micro\Base\IContainer;
6
use Micro\Db\IConnection;
7
use Micro\Mvc\Models\Query;
8
9
/**
10
 * Class DbDriver
11
 *
12
 * @author Oleg Lunegov <[email protected]>
13
 * @link https://github.com/linpax/microphp-framework
14
 * @copyright Copyright &copy; 2013 Oleg Lunegov
15
 * @license /LICENSE
16
 * @package Micro
17
 * @subpackage Cache\Driver
18
 * @version 1.0
19
 * @since 1.0
20
 */
21
class DbDriver extends BaseCacheDriver
22
{
23
    /** @var IConnection $driver DB driver */
24
    protected $driver;
25
    /** @var string $table table name */
26
    protected $table;
27
28
29
    /**
30
     * Constructor
31
     *
32
     * @access public
33
     *
34
     * @param IContainer $container
35
     * @param array $config config array
36
     *
37
     * @result void
38
     */
39
    public function __construct(IContainer $container, array $config = [])
40
    {
41
        parent::__construct($container, $config);
42
43
        $this->table = 'cache';
44
        if (!empty($config['table'])) {
45
            $this->table = $config['table'];
46
            unset($config['table']);
47
        }
48
49
        $cls = $config['class'];
50
        $this->driver = new $cls($config);
51
52
        $this->driver->createTable($this->table, [
53
            '`name` VARCHAR(127) NOT NULL',
54
            '`value` TEXT NULL',
55
            '`duration` INT(11) NOT NULL',
56
            '`date_create` INT(11) NOT NULL',
57
            'UNIQUE(`name`)'
58
        ], '');
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function check()
65
    {
66
        return ($this->driver instanceof IConnection) ?: false;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     * @throws \Micro\Base\Exception
72
     */
73
    public function get($name)
74
    {
75
        /** @var array $element */
76
        if ($element = $this->getElement($name)) {
77
            return array_key_exists('value', $element) ? $element['value'] : null;
78
        }
79
80
        return null;
81
    }
82
83
    /**
84
     * @inheritdoc
85
     * @throws \Micro\Base\Exception
86
     */
87 View Code Duplication
    protected function getElement($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $query = new Query($this->driver);
90
        $query->table = $this->table;
91
        $query->addWhere('`name`=:name');
92
        $query->params = ['name' => $name];
93
        $query->limit = 1;
94
        $query->single = true;
95
96
        return $query->run(\PDO::FETCH_ASSOC);
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function set($name, $value)
103
    {
104
        return $this->driver->update($this->table, ['`name`="'.$value.'"'], 'name="'.$name.'"');
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    public function delete($name)
111
    {
112
        return $this->driver->delete($this->table, 'name=:name', ['name' => $name]);
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function clean()
119
    {
120
        $this->driver->clearTable($this->table);
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function info()
127
    {
128
        return $this->driver->count(null, $this->table);
129
    }
130
131
    /**
132
     * @inheritdoc
133
     * @throws \Micro\Base\Exception
134
     */
135
    public function getMeta($id)
136
    {
137
        return $this->getElement($id);
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function increment($name, $offset = 1)
144
    {
145
        return $this->driver->update($this->table, ['value' => 'value+'.$offset], 'name="'.$name.'"');
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function decrement($name, $offset = 1)
152
    {
153
        return $this->driver->update($this->table, ['value' => 'value-'.$offset], 'name="'.$name.'"');
154
    }
155
} 
156