Completed
Push — V6 ( 198742...8f6ce3 )
by Georges
02:35
created

Driver::driverConnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
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 Lucas Brucksch <[email protected]>
11
 *
12
 */
13
14
namespace phpFastCache\Drivers\Zenddisk;
15
16
use phpFastCache\Core\Pool\DriverBaseTrait;
17
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
18
use phpFastCache\Entities\driverStatistic;
19
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
20
use phpFastCache\Exceptions\phpFastCacheDriverException;
21
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException;
22
use Psr\Cache\CacheItemInterface;
23
24
/**
25
 * Class Driver (zend disk cache)
26
 * Requires Zend Data Cache Functions from ZendServer
27
 * @package phpFastCache\Drivers
28
 */
29
class Driver implements ExtendedCacheItemPoolInterface
30
{
31
    use DriverBaseTrait;
32
    /**
33
     * Driver constructor.
34
     * @param array $config
35
     * @throws phpFastCacheDriverException
36
     */
37
    public function __construct(array $config = [])
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('Zend Data Cache') && function_exists('zend_disk_cache_store')) {
52
            return true;
53
        } else {
54
            return false;
55
        }
56
    }
57
58
    /**
59
     * @param \Psr\Cache\CacheItemInterface $item
60
     * @return mixed
61
     * @throws phpFastCacheInvalidArgumentException
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 zend_disk_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0));
72
        } else {
73
            throw new phpFastCacheInvalidArgumentException('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 = zend_disk_cache_fetch($item->getKey());
84
        if ($data === false) {
85
            return null;
86
        }
87
88
        return $data;
89
    }
90
91
    /**
92
     * @param \Psr\Cache\CacheItemInterface $item
93
     * @return bool
94
     * @throws phpFastCacheInvalidArgumentException
95
     */
96
    protected function driverDelete(CacheItemInterface $item)
97
    {
98
        /**
99
         * Check for Cross-Driver type confusion
100
         */
101
        if ($item instanceof Item) {
102
            return zend_disk_cache_delete($item->getKey());
103
        } else {
104
            throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected');
105
        }
106
    }
107
108
    /**
109
     * @return bool
110
     */
111
    protected function driverClear()
112
    {
113
        return @zend_disk_cache_clear();
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 string
132
     */
133
    public static function getHelp()
134
    {
135
        return <<<HELP
136
<p>
137
This driver rely on Zend Server 8.5+, see: http://www.zend.com/en/products/zend_server
138
</p>
139
HELP;
140
    }
141
142
    /**
143
     * @return driverStatistic
144
     */
145
    public function getStats()
146
    {
147
        $stat = new driverStatistic();
148
        $stat->setInfo('[ZendDisk] A void info string')
149
            ->setSize(0)
150
            ->setData(implode(', ', array_keys($this->itemInstances)))
151
            ->setRawData(false);
152
153
        return $stat;
154
    }
155
}