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

Driver   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 137
Duplicated Lines 16.06 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 137
rs 10
wmc 23
lcom 1
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A driverCheck() 0 4 1
A driverWrite() 13 13 2
A driverDelete() 0 11 2
A driverClear() 0 4 1
A driverRead() 9 9 2
F driverConnect() 0 27 12
A getStats() 0 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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