Completed
Push — V6 ( bd4c71...8eea0b )
by Georges
02:23
created

Driver::driverDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 9.4285
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\Pool\DriverBaseTrait;
18
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
19
use phpFastCache\Entities\driverStatistic;
20
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
21
use phpFastCache\Exceptions\phpFastCacheDriverException;
22
use Predis\Client as PredisClient;
23
use Psr\Cache\CacheItemInterface;
24
25
/**
26
 * Class Driver
27
 * @package phpFastCache\Drivers
28
 * @property PredisClient $instance Instance of driver service
29
 */
30
class Driver implements ExtendedCacheItemPoolInterface
31
{
32
    use DriverBaseTrait;
33
34
    /**
35
     * Driver constructor.
36
     * @param array $config
37
     * @throws phpFastCacheDriverException
38
     */
39
    public function __construct(array $config = [])
40
    {
41
        $this->setup($config);
42
43
        if (!$this->driverCheck()) {
44
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
45
        } else {
46
            $this->driverConnect();
47
        }
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function driverCheck()
54
    {
55
        if (extension_loaded('Redis')) {
56
            trigger_error('The native Redis extension is installed, you should use Redis instead of Predis to increase performances', E_USER_NOTICE);
57
        }
58
59
        return class_exists('Predis\Client');
60
    }
61
62
    /**
63
     * @param \Psr\Cache\CacheItemInterface $item
64
     * @return mixed
65
     * @throws \InvalidArgumentException
66
     */
67 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...
68
    {
69
        /**
70
         * Check for Cross-Driver type confusion
71
         */
72
        if ($item instanceof Item) {
73
            $ttl = $item->getExpirationDate()->getTimestamp() - time();
74
75
            return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
76
        } else {
77
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
78
        }
79
    }
80
81
    /**
82
     * @param \Psr\Cache\CacheItemInterface $item
83
     * @return mixed
84
     */
85 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...
86
    {
87
        $val = $this->instance->get($item->getKey());
88
        if ($val == false) {
89
            return null;
90
        } else {
91
            return $this->decode($val);
92
        }
93
    }
94
95
    /**
96
     * @param \Psr\Cache\CacheItemInterface $item
97
     * @return bool
98
     * @throws \InvalidArgumentException
99
     */
100
    protected function driverDelete(CacheItemInterface $item)
101
    {
102
        /**
103
         * Check for Cross-Driver type confusion
104
         */
105
        if ($item instanceof Item) {
106
            return $this->instance->del($item->getKey());
107
        } else {
108
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
109
        }
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    protected function driverClear()
116
    {
117
        return $this->instance->flushDB();
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    protected function driverConnect()
124
    {
125
        $config = isset($this->config[ 'redis' ]) ? $this->config[ 'redis' ] : [];
126
127
        $this->instance = new PredisClient(array_merge([
128
          'host' => '127.0.0.1',
129
          'port' => '6379',
130
          'password' => '',
131
          'database' => '',
132
        ], $config));
133
134
        return true;
135
    }
136
137
    /********************
138
     *
139
     * PSR-6 Extended Methods
140
     *
141
     *******************/
142
143
    /**
144
     * @return driverStatistic
145
     */
146
    public function getStats()
147
    {
148
        $info = $this->instance->info();
149
        $size = (isset($info['Memory']['used_memory']) ? $info['Memory']['used_memory'] : 0);
150
        $version = (isset($info['Server']['redis_version']) ? $info['Server']['redis_version'] : 0);
151
        $date = (isset($info['Server'][ 'uptime_in_seconds' ]) ? (new \DateTime())->setTimestamp(time() - $info['Server'][ 'uptime_in_seconds' ]) : 'unknown date');
152
153
        return (new driverStatistic())
154
          ->setData(implode(', ', array_keys($this->itemInstances)))
155
          ->setRawData($this->instance->info())
156
          ->setSize($size)
157
          ->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.", $version, $date->format(DATE_RFC2822)));
158
    }
159
}