Completed
Push — v5 ( a234cb...4f37c0 )
by Georges
02:54
created

Driver::driverCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 phpFastCache\Core\DriverAbstract;
18
use phpFastCache\Core\PathSeekerTrait;
19
use phpFastCache\Core\StandardPsr6StructureTrait;
20
use phpFastCache\Entities\driverStatistic;
21
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
22
use phpFastCache\Exceptions\phpFastCacheDriverException;
23
use phpFastCache\Util\Directory;
24
use Psr\Cache\CacheItemInterface;
25
use LevelDB as LeveldbClient;
26
27
/**
28
 * Class Driver
29
 * @package phpFastCache\Drivers
30
 */
31
class Driver extends DriverAbstract
32
{
33
    use PathSeekerTrait, StandardPsr6StructureTrait;
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 View Code Duplication
    public function __construct(array $config = [])
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...
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) {
1 ignored issue
show
Bug introduced by
The class LevelDB does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
129
            $this->instance->close();
130
            unset($this->instance);
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) {
1 ignored issue
show
Bug introduced by
The class LevelDB does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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
        $this->instance->close();
173
    }
174
}