Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

WincacheCache   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 113
Duplicated Lines 12.39 %

Coupling/Cohesion

Components 0
Dependencies 2
Metric Value
wmc 16
lcom 0
cbo 2
dl 14
loc 113
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A check() 0 4 2
A get() 0 7 2
A set() 0 4 1
A delete() 0 4 1
A clean() 0 4 1
A info() 0 4 1
A getMeta() 0 12 2
A increment() 7 7 2
A decrement() 7 7 2

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 /** MicroWincacheCache */
2
3
namespace Micro\Cache;
4
5
use Micro\Base\Exception;
6
7
/**
8
 * Class WincacheCache
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/lugnsk/micro
12
 * @copyright Copyright &copy; 2013 Oleg Lunegov
13
 * @license /LICENSE
14
 * @package Micro
15
 * @subpackage Cache
16
 * @version 1.0
17
 * @since 1.0
18
 */
19
class WincacheCache extends BaseCache
20
{
21
    /**
22
     * Constructor
23
     *
24
     * @access public
25
     *
26
     * @param array $config array config
27
     *
28
     * @result void
29
     * @throws Exception
30
     */
31
    public function __construct(array $config = [])
32
    {
33
        parent::__construct($config);
34
35
        if (!$this->check()) {
36
            throw new Exception('Extension WinCache not installed');
37
        }
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function check()
44
    {
45
        return (!extension_loaded('wincache')) ? true : false;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function get($name)
52
    {
53
        $success = false;
54
        $data = wincache_ucache_get($name, $success);
55
56
        return ($success) ? $data : false;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function set($name, $value, $duration = 0)
63
    {
64
        return wincache_ucache_set($name, $value, $duration);
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function delete($name)
71
    {
72
        return wincache_ucache_delete($name);
73
    }
74
75
    /**
76
     * Clean all data from cache
77
     *
78
     * @access public
79
     * @return mixed
80
     */
81
    public function clean()
82
    {
83
        return wincache_ucache_clear();
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function info()
90
    {
91
        return wincache_ucache_info(true);
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function getMeta($id)
98
    {
99
        if ($stored = wincache_ucache_info(false, $id)) {
100
            $age = $stored['ucache_entries'][1]['age_seconds'];
101
            $ttl = $stored['ucache_entries'][1]['ttl_seconds'];
102
            $hitCount = $stored['ucache_entries'][1]['hitcount'];
103
104
            return ['expire' => $ttl - $age, 'hitcount' => $hitCount, 'age' => $age, 'ttl' => $ttl];
105
        }
106
107
        return false;
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 View Code Duplication
    public function increment($name, $offset = 1)
0 ignored issues
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...
114
    {
115
        $success = false;
116
        $value = wincache_ucache_inc($name, $offset, $success);
117
118
        return ($success === true) ? $value : false;
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124 View Code Duplication
    public function decrement($name, $offset = 1)
0 ignored issues
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...
125
    {
126
        $success = false;
127
        $value = wincache_ucache_dec($name, $offset, $success);
128
129
        return ($success === true) ? $value : false;
130
    }
131
} 
132