Completed
Pull Request — final (#299)
by Georges
03:16
created

Driver   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 147
Duplicated Lines 6.12 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 9
Bugs 6 Features 1
Metric Value
c 9
b 6
f 1
dl 9
loc 147
rs 10
wmc 18
lcom 1
cbo 7

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getLeveldbFile() 0 4 1
A driverCheck() 0 4 1
A driverWrite() 0 11 2
A driverDelete() 0 11 2
A driverClear() 0 11 2
A driverConnect() 0 8 3
A driverRead() 9 9 2
A getStats() 0 7 1
A __destruct() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
15
namespace phpFastCache\Drivers\Leveldb;
16
17
use LevelDB as LeveldbClient;
18
use phpFastCache\Core\DriverAbstract;
19
use phpFastCache\Core\PathSeekerTrait;
20
use phpFastCache\Core\StandardPsr6StructureTrait;
21
use phpFastCache\Entities\driverStatistic;
22
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
23
use phpFastCache\Exceptions\phpFastCacheDriverException;
24
use phpFastCache\Util\Directory;
25
use Psr\Cache\CacheItemInterface;
26
27
/**
28
 * Class Driver
29
 * @package phpFastCache\Drivers
30
 */
31
class Driver extends DriverAbstract
32
{
33
    use PathSeekerTrait;
34
35
    const LEVELDB_FILENAME = '.database';
36
37
    /**
38
     * @var LeveldbClient Instance of driver service
39
     */
40
    public $instance;
41
42
    /**
43
     * Driver constructor.
44
     * @param array $config
45
     * @throws phpFastCacheDriverException
46
     */
47
    public function __construct(array $config = [])
48
    {
49
        $this->setup($config);
50
51
        if (!$this->driverCheck()) {
52
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
53
        } else {
54
            $this->driverConnect();
55
        }
56
    }
57
58
    /**
59
     * @return string
60
     * @throws \phpFastCache\Exceptions\phpFastCacheCoreException
61
     */
62
    public function getLeveldbFile()
63
    {
64
        return $this->getPath() . '/' . self::LEVELDB_FILENAME;
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function driverCheck()
71
    {
72
        return extension_loaded('Leveldb');
73
    }
74
75
    /**
76
     * @param \Psr\Cache\CacheItemInterface $item
77
     * @return mixed
78
     * @throws \InvalidArgumentException
79
     */
80
    protected function driverWrite(CacheItemInterface $item)
81
    {
82
        /**
83
         * Check for Cross-Driver type confusion
84
         */
85
        if ($item instanceof Item) {
86
            return $this->instance->set($item->getKey(), $this->encode($this->driverPreWrap($item)));
87
        } else {
88
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
89
        }
90
    }
91
92
    /**
93
     * @param \Psr\Cache\CacheItemInterface $item
94
     * @return mixed
95
     */
96 View Code Duplication
    protected function driverRead(CacheItemInterface $item)
1 ignored issue
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...
97
    {
98
        $val = $this->instance->get($item->getKey());
99
        if ($val == false) {
100
            return null;
101
        } else {
102
            return $this->decode($val);
103
        }
104
    }
105
106
    /**
107
     * @param \Psr\Cache\CacheItemInterface $item
108
     * @return bool
109
     * @throws \InvalidArgumentException
110
     */
111
    protected function driverDelete(CacheItemInterface $item)
112
    {
113
        /**
114
         * Check for Cross-Driver type confusion
115
         */
116
        if ($item instanceof Item) {
117
            return $this->instance->delete($item->getKey());
118
        } else {
119
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
120
        }
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    protected function driverClear()
127
    {
128
        if ($this->instance instanceof LeveldbClient) {
129
            $this->instance->close();
130
            $this->instance = null;
131
        }
132
        $result = LeveldbClient::destroy($this->getLeveldbFile());
133
        $this->driverConnect();
134
135
        return $result;
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    protected function driverConnect()
142
    {
143
        if ($this->instance instanceof LeveldbClient) {
144
            throw new \LogicException('Already connected to Leveldb database');
145
        } else {
146
            $this->instance = $this->instance ?: new LeveldbClient($this->getLeveldbFile());
147
        }
148
    }
149
150
    /********************
151
     *
152
     * PSR-6 Extended Methods
153
     *
154
     *******************/
155
156
    /**
157
     * @return driverStatistic
158
     */
159
    public function getStats()
160
    {
161
        return (new driverStatistic())
162
          ->setData(implode(', ', array_keys($this->itemInstances)))
163
          ->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($this->getLeveldbFile()))
164
          ->setSize(Directory::dirSize($this->getLeveldbFile()));
165
    }
166
167
    /**
168
     * Close connection on destruct
169
     */
170
    public function __destruct()
171
    {
172
        if ($this->instance instanceof LeveldbClient) {
173
            $this->instance->close();
174
            $this->instance = null;
175
        }
176
    }
177
}