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

Driver::driverCheck()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
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\Xcache;
16
17
use phpFastCache\Core\StandardPsr6StructureTrait;
18
use phpFastCache\Core\DriverAbstract;
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
        return extension_loaded('xcache') && function_exists('xcache_get');
52
    }
53
54
    /**
55
     * @param \Psr\Cache\CacheItemInterface $item
56
     * @return mixed
57
     * @throws \InvalidArgumentException
58
     */
59
    protected function driverWrite(CacheItemInterface $item)
60
    {
61
        /**
62
         * Check for Cross-Driver type confusion
63
         */
64
        if ($item instanceof Item) {
65
            return xcache_set($item->getKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl());
66
67
        } else {
68
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
69
        }
70
    }
71
72
    /**
73
     * @param \Psr\Cache\CacheItemInterface $item
74
     * @return mixed
75
     */
76
    protected function driverRead(CacheItemInterface $item)
77
    {
78
        $data = $this->decode(xcache_get($item->getKey()));
79
        if ($data === false || $data === '') {
80
            return null;
81
        }
82
83
        return $data;
84
    }
85
86
    /**
87
     * @param \Psr\Cache\CacheItemInterface $item
88
     * @return bool
89
     * @throws \InvalidArgumentException
90
     */
91
    protected function driverDelete(CacheItemInterface $item)
92
    {
93
        /**
94
         * Check for Cross-Driver type confusion
95
         */
96
        if ($item instanceof Item) {
97
            return xcache_unset($item->getKey());
98
        } else {
99
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
100
        }
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    protected function driverClear()
107
    {
108
        $cnt = xcache_count(XC_TYPE_VAR);
109
        for ($i = 0; $i < $cnt; $i++) {
110
            xcache_clear_cache(XC_TYPE_VAR, $i);
111
        }
112
113
        return true;
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
        if(!ini_get('xcache.admin.enable_auth'))
136
        {
137
            $info = xcache_info(XC_TYPE_VAR, 0);
138
139
            return (new driverStatistic())
140
              ->setSize(abs($info['size'] - $info['avail']))
141
              ->setData(implode(', ', array_keys($this->itemInstances)))
142
              ->setInfo(sprintf("Xcache v%s with following modules loaded:\n %s", XCACHE_VERSION, str_replace(' ', ', ', XCACHE_MODULES)))
143
              ->setRawData($info);
144
        }
145
        else
146
        {
147
            throw new \RuntimeException("PhpFastCache is not able to read Xcache configuration. Please put this to your php.ini:\n
148
            [xcache.admin]
149
            xcache.admin.enable_auth = Off\n
150
            Then reboot your webserver and make sure that the native Xcache ini configuration file does not override your setting.");
151
        }
152
    }
153
}