Completed
Push — v5 ( a234cb...4f37c0 )
by Georges
02:54
created

Driver   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 116
Duplicated Lines 6.9 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 9
Bugs 3 Features 3
Metric Value
c 9
b 3
f 3
dl 8
loc 116
rs 10
wmc 16
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A driverCheck() 0 8 3
A driverWrite() 0 13 3
A driverDelete() 0 11 2
A driverClear() 0 4 2
A driverConnect() 0 4 1
A driverRead() 0 9 2
A getStats() 0 10 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\Apc;
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
24
/**
25
 * Class Driver
26
 * @package phpFastCache\Drivers
27
 */
28
class Driver extends DriverAbstract
29
{
30
    use StandardPsr6StructureTrait;
31
32
    /**
33
     * Driver constructor.
34
     * @param array $config
35
     * @throws phpFastCacheDriverException
36
     */
37 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...
38
    {
39
        $this->setup($config);
40
41
        if (!$this->driverCheck()) {
42
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
43
        }
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    public function driverCheck()
50
    {
51
        if (extension_loaded('apc') && ini_get('apc.enabled')) {
52
            return true;
53
        } else {
54
            return false;
55
        }
56
    }
57
58
    /**
59
     * @param \Psr\Cache\CacheItemInterface $item
60
     * @return mixed
61
     * @throws \InvalidArgumentException
62
     */
63
    protected function driverWrite(CacheItemInterface $item)
64
    {
65
        /**
66
         * Check for Cross-Driver type confusion
67
         */
68
        if ($item instanceof Item) {
69
            $ttl = $item->getExpirationDate()->getTimestamp() - time();
70
71
            return apc_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0));
72
        } else {
73
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
74
        }
75
    }
76
77
    /**
78
     * @param \Psr\Cache\CacheItemInterface $item
79
     * @return mixed
80
     */
81
    protected function driverRead(CacheItemInterface $item)
82
    {
83
        $data = apc_fetch($item->getKey(), $success);
84
        if ($success === false) {
85
            return null;
86
        }
87
88
        return $data;
89
    }
90
91
    /**
92
     * @param \Psr\Cache\CacheItemInterface $item
93
     * @return bool
94
     * @throws \InvalidArgumentException
95
     */
96
    protected function driverDelete(CacheItemInterface $item)
97
    {
98
        /**
99
         * Check for Cross-Driver type confusion
100
         */
101
        if ($item instanceof Item) {
102
            return apc_delete($item->getKey());
103
        } else {
104
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
105
        }
106
    }
107
108
    /**
109
     * @return bool
110
     */
111
    protected function driverClear()
112
    {
113
        return @apc_clear_cache() && @apc_clear_cache('user');
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    protected function driverConnect()
120
    {
121
        return true;
122
    }
123
124
    /********************
125
     *
126
     * PSR-6 Extended Methods
127
     *
128
     *******************/
129
130
    /**
131
     * @return driverStatistic
132
     */
133
    public function getStats()
134
    {
135
        $stats = (array) apc_cache_info('user');
136
        $date = (new \DateTime())->setTimestamp($stats['start_time']);
137
        return (new driverStatistic())
138
          ->setData(implode(', ', array_keys($this->itemInstances)))
139
          ->setInfo(sprintf("The APC cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.", $date->format(DATE_RFC2822), $stats['num_entries']))
140
          ->setRawData($stats)
141
          ->setSize($stats['mem_size']);
142
    }
143
}