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

Driver::driverIsHit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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\Ssdb;
16
17
use phpFastCache\Core\DriverAbstract;
18
use phpFastCache\Core\StandardPsr6StructureTrait;
19
use phpFastCache\Entities\driverStatistic;
20
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
21
use phpFastCache\Exceptions\phpFastCacheDriverException;
22
use phpssdb\Core\SimpleSSDB;
23
use phpssdb\Core\SSDB;
24
use phpssdb\Core\SSDBException;
25
use Psr\Cache\CacheItemInterface;
26
27
/**
28
 * Class Driver
29
 * @package phpFastCache\Drivers
30
 */
31
class Driver extends DriverAbstract
32
{
33
    use StandardPsr6StructureTrait;
34
35
    /**
36
     * @var SimpleSSDB
37
     */
38
    public $instance;
39
40
    /**
41
     * Driver constructor.
42
     * @param array $config
43
     * @throws phpFastCacheDriverException
44
     */
45 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...
46
    {
47
        $this->setup($config);
48
49
        if (!$this->driverCheck()) {
50
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
51
        } elseif (!$this->driverConnect()) {
52
            throw new phpFastCacheDriverException('Ssdb is not connected, cannot continue.');
53
        }
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    public function driverCheck()
60
    {
61
        static $driverCheck;
62
        if ($driverCheck === null) {
63
            return ($driverCheck = class_exists('phpssdb\Core\SSDB'));
64
        }
65
66
        return $driverCheck;
67
    }
68
69
    /**
70
     * @param \Psr\Cache\CacheItemInterface $item
71
     * @return mixed
72
     * @throws \InvalidArgumentException
73
     */
74 View Code Duplication
    protected function driverWrite(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...
75
    {
76
        /**
77
         * Check for Cross-Driver type confusion
78
         */
79
        if ($item instanceof Item) {
80
            /*            if (isset($this->config[ 'skipExisting' ]) && $this->config[ 'skipExisting' ] == true) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
                            $x = $this->instance->get($item->getKey());
82
                            if ($x === false) {
83
                                return false;
84
                            } elseif (!is_null($x)) {
85
                                return true;
86
                            }
87
                        }*/
88
89
            return $this->instance->setx($item->getKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl());
90
        } else {
91
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
92
        }
93
    }
94
95
    /**
96
     * @param \Psr\Cache\CacheItemInterface $item
97
     * @return mixed
98
     */
99 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...
100
    {
101
        $val = $this->instance->get($item->getKey());
102
        if ($val == false) {
103
            return null;
104
        } else {
105
            return $this->decode($val);
106
        }
107
    }
108
109
    /**
110
     * @param \Psr\Cache\CacheItemInterface $item
111
     * @return bool
112
     * @throws \InvalidArgumentException
113
     */
114
    protected function driverDelete(CacheItemInterface $item)
115
    {
116
        /**
117
         * Check for Cross-Driver type confusion
118
         */
119
        if ($item instanceof Item) {
120
            return $this->instance->del($item->get());
121
        } else {
122
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
123
        }
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    protected function driverClear()
130
    {
131
        $return = null;
132
        foreach ($this->instance->keys('', '') as $key) {
133
            $result = $this->instance->del($key);
134
            if ($result !== false) {
135
                $return = $result;
136
            }
137
        }
138
139
        return $return;
140
    }
141
142
    /**
143
     * @return bool
144
     * @throws phpFastCacheDriverException
145
     */
146
    protected function driverConnect()
147
    {
148
        try{
149
            $server = isset($this->config[ 'ssdb' ]) ? $this->config[ 'ssdb' ] : [
150
              'host' => "127.0.0.1",
151
              'port' => 8888,
152
              'password' => '',
153
              'timeout' => 2000,
154
            ];
155
156
            $host = $server[ 'host' ];
157
            $port = isset($server[ 'port' ]) ? (int) $server[ 'port' ] : 8888;
158
            $password = isset($server[ 'password' ]) ? $server[ 'password' ] : '';
159
            $timeout = !empty($server[ 'timeout' ]) ? (int) $server[ 'timeout' ] : 2000;
160
            $this->instance = new SimpleSSDB($host, $port, $timeout);
161
            if (!empty($password)) {
162
                $this->instance->auth($password);
163
            }
164
165
            if (!$this->instance) {
166
                return false;
167
            } else {
168
                return true;
169
            }
170
        }catch(SSDBException $e){
171
            throw new phpFastCacheDriverException('Ssdb failed to connect with error: '. $e->getMessage(), 0 , $e);
172
        }
173
    }
174
175
    /********************
176
     *
177
     * PSR-6 Extended Methods
178
     *
179
     *******************/
180
181
    /**
182
     * @return driverStatistic
183
     */
184
    public function getStats()
185
    {
186
        $stat = new driverStatistic();
187
        $info = $this->instance->info();
188
189
        /**
190
         * Data returned by Ssdb are very poorly formatted
191
         * using hardcoded offset of pair key-value :-(
192
         */
193
        $stat->setInfo(sprintf("Ssdb-server v%s with a total of %s call(s).\n For more information see RawData.", $info[2], $info[6]))
194
            ->setRawData($info)
195
            ->setData(implode(', ', array_keys($this->itemInstances)))
196
            ->setSize($this->instance->dbsize());
197
198
        return $stat;
199
    }
200
}