Passed
Pull Request — master (#857)
by Georges
01:55
created

Driver::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author Lucas Brucksch <[email protected]>
12
 *
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Drivers\Zenddisk;
18
19
use Phpfastcache\Cluster\AggregatablePoolInterface;
20
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
21
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
22
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
23
use Phpfastcache\Entities\DriverStatistic;
24
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
25
26
/**
27
 * Requires Zend Data Cache Functions from ZendServer
28
 * @method Config getConfig()
29
 */
30
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
31
{
32
    use TaggableCacheItemPoolTrait;
33
34
    /**
35
     * @return bool
36
     */
37
    public function driverCheck(): bool
38
    {
39
        return extension_loaded('Zend Data Cache') && function_exists('zend_disk_cache_store');
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getHelp(): string
46
    {
47
        return <<<HELP
48
<p>
49
This driver rely on Zend Server 8.5+, see: https://www.zend.com/en/products/zend_server
50
</p>
51
HELP;
52
    }
53
54
    /**
55
     * @return DriverStatistic
56
     */
57
    public function getStats(): DriverStatistic
58
    {
59
        $stat = new DriverStatistic();
60
        $stat->setInfo('[ZendDisk] A void info string')
61
            ->setSize(0)
62
            ->setData(implode(', ', array_keys($this->itemInstances)))
63
            ->setRawData(false);
64
65
        return $stat;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    protected function driverConnect(): bool
72
    {
73
        return true;
74
    }
75
76
    /**
77
     * @param ExtendedCacheItemInterface $item
78
     * @return null|array
79
     */
80
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
81
    {
82
        $data = zend_disk_cache_fetch($item->getKey());
83
        if ($data === false) {
84
            return null;
85
        }
86
87
        return $data;
88
    }
89
90
    /**
91
     * @param ExtendedCacheItemInterface $item
92
     * @return mixed
93
     * @throws PhpfastcacheInvalidArgumentException
94
     */
95
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
96
    {
97
        $this->assertCacheItemType($item, Item::class);
98
99
        $ttl = $item->getExpirationDate()->getTimestamp() - time();
100
101
        return zend_disk_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0));
102
    }
103
104
    /**
105
     * @param ExtendedCacheItemInterface $item
106
     * @return bool
107
     * @throws PhpfastcacheInvalidArgumentException
108
     */
109
    protected function driverDelete(ExtendedCacheItemInterface $item): bool
110
    {
111
        $this->assertCacheItemType($item, Item::class);
112
113
        return (bool)zend_disk_cache_delete($item->getKey());
114
    }
115
116
117
    protected function driverClear(): bool
118
    {
119
        return @zend_disk_cache_clear();
120
    }
121
}
122