Completed
Branch final (ad8b8d)
by Georges
03:08 queued 28s
created

Driver::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
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
    /**
34
     * @var SimpleSSDB
35
     */
36
    public $instance;
37
38
    /**
39
     * Driver constructor.
40
     * @param array $config
41
     * @throws phpFastCacheDriverException
42
     */
43 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...
44
    {
45
        $this->setup($config);
46
47
        if (!$this->driverCheck()) {
48
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
49
        } elseif (!$this->driverConnect()) {
50
            throw new phpFastCacheDriverException('Ssdb is not connected, cannot continue.');
51
        }
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function driverCheck()
58
    {
59
        static $driverCheck;
60
        if ($driverCheck === null) {
61
            return ($driverCheck = class_exists('phpssdb\Core\SSDB'));
62
        }
63
64
        return $driverCheck;
65
    }
66
67
    /**
68
     * @param \Psr\Cache\CacheItemInterface $item
69
     * @return mixed
70
     * @throws \InvalidArgumentException
71
     */
72 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...
73
    {
74
        /**
75
         * Check for Cross-Driver type confusion
76
         */
77
        if ($item instanceof Item) {
78
            /*            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...
79
                            $x = $this->instance->get($item->getKey());
80
                            if ($x === false) {
81
                                return false;
82
                            } elseif (!is_null($x)) {
83
                                return true;
84
                            }
85
                        }*/
86
87
            return $this->instance->setx($item->getKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl());
88
        } else {
89
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
90
        }
91
    }
92
93
    /**
94
     * @param \Psr\Cache\CacheItemInterface $item
95
     * @return mixed
96
     */
97 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...
98
    {
99
        $val = $this->instance->get($item->getKey());
100
        if ($val == false) {
101
            return null;
102
        } else {
103
            return $this->decode($val);
104
        }
105
    }
106
107
    /**
108
     * @param \Psr\Cache\CacheItemInterface $item
109
     * @return bool
110
     * @throws \InvalidArgumentException
111
     */
112
    protected function driverDelete(CacheItemInterface $item)
113
    {
114
        /**
115
         * Check for Cross-Driver type confusion
116
         */
117
        if ($item instanceof Item) {
118
            return $this->instance->del($item->get());
119
        } else {
120
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
121
        }
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    protected function driverClear()
128
    {
129
        return $this->instance->flushdb('kv');
130
    }
131
132
    /**
133
     * @return bool
134
     * @throws phpFastCacheDriverException
135
     */
136
    protected function driverConnect()
137
    {
138
        try {
139
            $server = isset($this->config[ 'ssdb' ]) ? $this->config[ 'ssdb' ] : [
140
              'host' => "127.0.0.1",
141
              'port' => 8888,
142
              'password' => '',
143
              'timeout' => 2000,
144
            ];
145
146
            $host = $server[ 'host' ];
147
            $port = isset($server[ 'port' ]) ? (int) $server[ 'port' ] : 8888;
148
            $password = isset($server[ 'password' ]) ? $server[ 'password' ] : '';
149
            $timeout = !empty($server[ 'timeout' ]) ? (int) $server[ 'timeout' ] : 2000;
150
            $this->instance = new SimpleSSDB($host, $port, $timeout);
151
            if (!empty($password)) {
152
                $this->instance->auth($password);
153
            }
154
155
            if (!$this->instance) {
156
                return false;
157
            } else {
158
                return true;
159
            }
160
        } catch (SSDBException $e) {
161
            throw new phpFastCacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e);
162
        }
163
    }
164
165
    /********************
166
     *
167
     * PSR-6 Extended Methods
168
     *
169
     *******************/
170
171
    /**
172
     * @return driverStatistic
173
     */
174
    public function getStats()
175
    {
176
        $stat = new driverStatistic();
177
        $info = $this->instance->info();
178
179
        /**
180
         * Data returned by Ssdb are very poorly formatted
181
         * using hardcoded offset of pair key-value :-(
182
         */
183
        $stat->setInfo(sprintf("Ssdb-server v%s with a total of %s call(s).\n For more information see RawData.", $info[ 2 ], $info[ 6 ]))
184
          ->setRawData($info)
185
          ->setData(implode(', ', array_keys($this->itemInstances)))
186
          ->setSize($this->instance->dbsize());
187
188
        return $stat;
189
    }
190
}