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

Driver::driverClear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 4
rs 10
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\Apcu;
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('apcu') && 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 APCU 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
}