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

Driver::driverConnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Wincache;
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
    /**
31
     * Driver constructor.
32
     * @param array $config
33
     * @throws phpFastCacheDriverException
34
     */
35
    public function __construct(array $config = [])
36
    {
37
        $this->setup($config);
38
39
        if (!$this->driverCheck()) {
40
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
41
        }
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function driverCheck()
48
    {
49
        return extension_loaded('wincache') && function_exists('wincache_ucache_set');
50
    }
51
52
    /**
53
     * @param \Psr\Cache\CacheItemInterface $item
54
     * @return mixed
55
     * @throws \InvalidArgumentException
56
     */
57
    protected function driverWrite(CacheItemInterface $item)
58
    {
59
        /**
60
         * Check for Cross-Driver type confusion
61
         */
62
        if ($item instanceof Item) {
63
            return wincache_ucache_set($item->getKey(), $this->driverPreWrap($item), $item->getTtl());
64
        } else {
65
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
66
        }
67
    }
68
69
    /**
70
     * @param \Psr\Cache\CacheItemInterface $item
71
     * @return mixed
72
     */
73
    protected function driverRead(CacheItemInterface $item)
74
    {
75
        $val = wincache_ucache_get($item->getKey(), $suc);
0 ignored issues
show
Bug introduced by
The variable $suc does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
76
77
        if ($suc === false) {
78
            return null;
79
        } else {
80
            return $val;
81
        }
82
    }
83
84
    /**
85
     * @param \Psr\Cache\CacheItemInterface $item
86
     * @return bool
87
     * @throws \InvalidArgumentException
88
     */
89
    protected function driverDelete(CacheItemInterface $item)
90
    {
91
        /**
92
         * Check for Cross-Driver type confusion
93
         */
94
        if ($item instanceof Item) {
95
            return wincache_ucache_delete($item->getKey());
96
        } else {
97
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
98
        }
99
    }
100
101
    /**
102
     * @return bool
103
     */
104
    protected function driverClear()
105
    {
106
        return wincache_ucache_clear();
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    protected function driverConnect()
113
    {
114
        return true;
115
    }
116
117
    /********************
118
     *
119
     * PSR-6 Extended Methods
120
     *
121
     *******************/
122
123
    /**
124
     * @return driverStatistic
125
     */
126
    public function getStats()
127
    {
128
        $memInfo = wincache_ucache_meminfo();
129
        $info = wincache_ucache_info();
130
        $date = (new \DateTime())->setTimestamp(time() - $info[ 'total_cache_uptime' ]);
131
132
        return (new driverStatistic())
133
          ->setInfo(sprintf("The Wincache daemon is up since %s.\n For more information see RawData.", $date->format(DATE_RFC2822)))
134
          ->setSize($memInfo[ 'memory_free' ] - $memInfo[ 'memory_total' ])
135
          ->setData(implode(', ', array_keys($this->itemInstances)))
136
          ->setRawData($memInfo);
137
    }
138
}