Completed
Push — v5.1 ( 1e2251...5d1cda )
by Georges
03:25
created

Driver   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 143
Duplicated Lines 22.38 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 10
Bugs 3 Features 3
Metric Value
c 10
b 3
f 3
dl 32
loc 143
rs 10
wmc 22
lcom 2
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A driverCheck() 0 4 1
A driverWrite() 0 11 2
A driverDelete() 0 11 2
A driverClear() 0 4 1
A driverRead() 10 10 2
C driverConnect() 22 22 9
A getStats() 0 11 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\Memcache;
16
17
use Memcache as MemcacheSoftware;
18
use phpFastCache\Core\Pool\DriverBaseTrait;
19
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
20
use phpFastCache\Entities\driverStatistic;
21
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
22
use phpFastCache\Exceptions\phpFastCacheDriverException;
23
use phpFastCache\Util\MemcacheDriverCollisionDetectorTrait;
24
use Psr\Cache\CacheItemInterface;
25
26
/**
27
 * Class Driver
28
 * @package phpFastCache\Drivers
29
 */
30
class Driver implements ExtendedCacheItemPoolInterface
31
{
32
    use DriverBaseTrait, MemcacheDriverCollisionDetectorTrait;
33
34
    /**
35
     * @var int
36
     */
37
    protected $memcacheFlags = 0;
38
39
    /**
40
     * Driver constructor.
41
     * @param array $config
42
     * @throws phpFastCacheDriverException
43
     */
44
    public function __construct(array $config = [])
45
    {
46
        self::checkCollision('Memcache');
47
        $this->setup($config);
48
49
        if (!$this->driverCheck()) {
50
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
51
        } else {
52
            $this->instance = new MemcacheSoftware();
53
            $this->driverConnect();
54
55
            if (array_key_exists('compress_data', $config) && $config[ 'compress_data' ] === true) {
56
                $this->memcacheFlags = MEMCACHE_COMPRESSED;
57
            }
58
        }
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    public function driverCheck()
65
    {
66
        return class_exists('Memcache');
67
    }
68
69
    /**
70
     * @param \Psr\Cache\CacheItemInterface $item
71
     * @return mixed
72
     * @throws \InvalidArgumentException
73
     */
74
    protected function driverWrite(CacheItemInterface $item)
75
    {
76
        /**
77
         * Check for Cross-Driver type confusion
78
         */
79
        if ($item instanceof Item) {
80
            return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $item->getTtl());
81
        } else {
82
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
83
        }
84
    }
85
86
    /**
87
     * @param \Psr\Cache\CacheItemInterface $item
88
     * @return mixed
89
     */
90 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...
91
    {
92
        $val = $this->instance->get($item->getKey());
93
94
        if ($val === false) {
95
            return null;
96
        } else {
97
            return $val;
98
        }
99
    }
100
101
    /**
102
     * @param \Psr\Cache\CacheItemInterface $item
103
     * @return bool
104
     * @throws \InvalidArgumentException
105
     */
106
    protected function driverDelete(CacheItemInterface $item)
107
    {
108
        /**
109
         * Check for Cross-Driver type confusion
110
         */
111
        if ($item instanceof Item) {
112
            return $this->instance->delete($item->getKey());
113
        } else {
114
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
115
        }
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    protected function driverClear()
122
    {
123
        return $this->instance->flush();
124
    }
125
126
    /**
127
     * @return bool
128
     */
129 View Code Duplication
    protected function driverConnect()
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...
130
    {
131
        $servers = (!empty($this->config[ 'memcache' ]) && is_array($this->config[ 'memcache' ]) ? $this->config[ 'memcache' ] : []);
132
        if (count($servers) < 1) {
133
            $servers = [
134
              ['127.0.0.1', 11211],
135
            ];
136
        }
137
138
        foreach ($servers as $server) {
139
            try {
140
                if (!$this->instance->addserver($server[ 0 ], $server[ 1 ])) {
141
                    $this->fallback = true;
142
                }
143
                if(!empty($server[ 'sasl_user' ]) && !empty($server[ 'sasl_password'])){
144
                    $this->instance->setSaslAuthData($server[ 'sasl_user' ], $server[ 'sasl_password']);
145
                }
146
            } catch (\Exception $e) {
147
                $this->fallback = true;
148
            }
149
        }
150
    }
151
152
    /********************
153
     *
154
     * PSR-6 Extended Methods
155
     *
156
     *******************/
157
158
    /**
159
     * @return driverStatistic
160
     */
161
    public function getStats()
162
    {
163
        $stats = (array) $this->instance->getstats();
164
        $date = (new \DateTime())->setTimestamp(time() - $stats[ 'uptime' ]);
165
166
        return (new driverStatistic())
167
          ->setData(implode(', ', array_keys($this->itemInstances)))
168
          ->setInfo(sprintf("The memcache daemon v%s is up since %s.\n For more information see RawData.", $stats[ 'version' ], $date->format(DATE_RFC2822)))
169
          ->setRawData($stats)
170
          ->setSize($stats[ 'bytes' ]);
171
    }
172
}