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 Method

Rating   Name   Duplication   Size   Complexity  
A Driver::getStats() 0 7 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\Predis;
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 Psr\Cache\CacheItemInterface;
23
use Predis\Client as PredisClient;
24
25
/**
26
 * Class Driver
27
 * @package phpFastCache\Drivers
28
 */
29
class Driver extends DriverAbstract
30
{
31
    use StandardPsr6StructureTrait;
32
33
    /**
34
     * @var PredisClient Instance of driver service
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
        } else {
50
            $this->driverConnect();
51
        }
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function driverCheck()
58
    {
59
        if (extension_loaded('Redis')) {
60
            trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE);
61
        }
62
63
        return class_exists('Predis\Client');
64
    }
65
66
    /**
67
     * @param \Psr\Cache\CacheItemInterface $item
68
     * @return mixed
69
     * @throws \InvalidArgumentException
70
     */
71 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...
72
    {
73
        /**
74
         * Check for Cross-Driver type confusion
75
         */
76
        if ($item instanceof Item) {
77
            $ttl = $item->getExpirationDate()->getTimestamp() - time();
78
79
            return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
80
        } else {
81
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
82
        }
83
    }
84
85
    /**
86
     * @param \Psr\Cache\CacheItemInterface $item
87
     * @return mixed
88
     */
89 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...
90
    {
91
        $val = $this->instance->get($item->getKey());
92
        if ($val == false) {
93
            return null;
94
        } else {
95
            return $this->decode($val);
96
        }
97
    }
98
99
    /**
100
     * @param \Psr\Cache\CacheItemInterface $item
101
     * @return bool
102
     * @throws \InvalidArgumentException
103
     */
104
    protected function driverDelete(CacheItemInterface $item)
105
    {
106
        /**
107
         * Check for Cross-Driver type confusion
108
         */
109
        if ($item instanceof Item) {
110
            return $this->instance->del($item->getKey());
111
        } else {
112
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
113
        }
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    protected function driverClear()
120
    {
121
        return $this->instance->flushDB();
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    protected function driverConnect()
128
    {
129
        $server = isset($this->config[ 'redis' ]) ? $this->config[ 'redis' ] : [
130
          'host' => '127.0.0.1',
131
          'port' => '6379',
132
          'password' => '',
133
          'database' => '',
134
        ];
135
136
        $config = [
137
          'host' => $server[ 'host' ],
138
        ];
139
140
        $port = isset($server[ 'port' ]) ? $server[ 'port' ] : '';
141
        if ($port != '') {
142
            $config[ 'port' ] = $port;
143
        }
144
145
        $password = isset($server[ 'password' ]) ? $server[ 'password' ] : '';
146
        if ($password != '') {
147
            $config[ 'password' ] = $password;
148
        }
149
150
        $database = isset($server[ 'database' ]) ? $server[ 'database' ] : '';
151
        if ($database != '') {
152
            $config[ 'database' ] = $database;
153
        }
154
155
        $timeout = isset($server[ 'timeout' ]) ? $server[ 'timeout' ] : '';
156
        if ($timeout != '') {
157
            $config[ 'timeout' ] = $timeout;
158
        }
159
160
        $read_write_timeout = isset($server[ 'read_write_timeout' ]) ? $server[ 'read_write_timeout' ] : '';
161
        if ($read_write_timeout != '') {
162
            $config[ 'read_write_timeout' ] = $read_write_timeout;
163
        }
164
165
        $this->instance = new PredisClient($config);
166
167
        return true;
168
    }
169
170
    /********************
171
     *
172
     * PSR-6 Extended Methods
173
     *
174
     *******************/
175
176
    /**
177
     * @return driverStatistic
178
     */
179
    public function getStats()
180
    {
181
        return (new driverStatistic())
182
          ->setRawData($this->instance->info())
183
          ->setSize(0)
184
          ->setInfo('');
185
    }
186
}