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\Redis;
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 Redis as RedisClient;
24
25
/**
26
 * Class Driver
27
 * @package phpFastCache\Drivers
28
 */
29
class Driver extends DriverAbstract
30
{
31
    use StandardPsr6StructureTrait;
32
33
    /**
34
     * Driver constructor.
35
     * @param array $config
36
     * @throws phpFastCacheDriverException
37
     */
38 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...
39
    {
40
        $this->setup($config);
41
42
        if (!$this->driverCheck()) {
43
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
44
        } else {
45
            $this->driverConnect();
46
        }
47
    }
48
49
    /**
50
     * @return bool
51
     */
52
    public function driverCheck()
53
    {
54
        return extension_loaded('Redis');
55
    }
56
57
    /**
58
     * @param \Psr\Cache\CacheItemInterface $item
59
     * @return mixed
60
     * @throws \InvalidArgumentException
61
     */
62 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...
63
    {
64
        /**
65
         * Check for Cross-Driver type confusion
66
         */
67
        if ($item instanceof Item) {
68
            $ttl = $item->getExpirationDate()->getTimestamp() - time();
69
70
            return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
71
        } else {
72
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
73
        }
74
    }
75
76
    /**
77
     * @param \Psr\Cache\CacheItemInterface $item
78
     * @return mixed
79
     */
80 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...
81
    {
82
        $val = $this->instance->get($item->getKey());
83
        if ($val == false) {
84
            return null;
85
        } else {
86
            return $this->decode($val);
87
        }
88
    }
89
90
    /**
91
     * @param \Psr\Cache\CacheItemInterface $item
92
     * @return bool
93
     * @throws \InvalidArgumentException
94
     */
95
    protected function driverDelete(CacheItemInterface $item)
96
    {
97
        /**
98
         * Check for Cross-Driver type confusion
99
         */
100
        if ($item instanceof Item) {
101
            return $this->instance->del($item->getKey());
102
        } else {
103
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
104
        }
105
    }
106
107
    /**
108
     * @return bool
109
     */
110
    protected function driverClear()
111
    {
112
        return $this->instance->flushDB();
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    protected function driverConnect()
119
    {
120
        if ($this->instance instanceof RedisClient) {
121
            throw new \LogicException('Already connected to Redis server');
122
        } else {
123
            $this->instance = $this->instance ?: new RedisClient();
124
125
            $host = isset($this->config[ 'host' ]) ? $this->config[ 'host' ] : '127.0.0.1';
126
            $port = isset($this->config[ 'port' ]) ? (int) $this->config[ 'port' ] : '6379';
127
            $password = isset($this->config[ 'password' ]) ? $this->config[ 'password' ] : '';
128
            $database = isset($this->config[ 'database' ]) ? $this->config[ 'database' ] : '';
129
            $timeout = isset($this->config[ 'timeout' ]) ? $this->config[ 'timeout' ] : '';
130
131
            if (!$this->instance->connect($host, (int) $port, (int) $timeout)) {
132
                return false;
133
            } else {
134
                if ($password && !$this->instance->auth($password)) {
135
                    return false;
136
                }
137
                if ($database) {
138
                    $this->instance->select((int) $database);
139
                }
140
141
                return true;
142
            }
143
        }
144
    }
145
146
    /********************
147
     *
148
     * PSR-6 Extended Methods
149
     *
150
     *******************/
151
152
    /**
153
     * @return driverStatistic
154
     */
155
    public function getStats()
156
    {
157
        // used_memory
158
        $info = $this->instance->info();
159
        $date = (new \DateTime())->setTimestamp(time() - $info['uptime_in_seconds']);
160
        return (new driverStatistic())
161
          ->setData(implode(', ', array_keys($this->itemInstances)))
162
          ->setRawData($info)
163
          ->setSize($info['used_memory'])
164
          ->setInfo(sprintf("The Redis daemon v%s is up since %s.\n For more information see RawData. \n Driver size includes the memory allocation size.", $info['redis_version'], $date->format(DATE_RFC2822)));
165
    }
166
}