Driver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 22
dl 0
loc 83
rs 10
c 3
b 1
f 1
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A driverCheck() 0 3 5
A driverConnect() 0 3 1
A getStats() 0 16 1
A driverRead() 0 9 3
A driverDelete() 0 3 1
A driverClear() 0 3 1
A driverWrite() 0 4 1
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 and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Drivers\Apcu;
18
19
use DateTime;
20
use Phpfastcache\Cluster\AggregatablePoolInterface;
21
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
22
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
23
use Phpfastcache\Config\ConfigurationOption;
24
use Phpfastcache\Config\ConfigurationOptionInterface;
25
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
26
use Phpfastcache\Entities\DriverStatistic;
27
use Phpfastcache\Util\SapiDetector;
28
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
29
30
/**
31
 * Class Driver
32
 * @method Config getConfig()
33
 */
34
class Driver implements AggregatablePoolInterface
35
{
36
    use TaggableCacheItemPoolTrait;
37
38
    /**
39
     * @return bool
40
     */
41
    public function driverCheck(): bool
42
    {
43
        return extension_loaded('apcu') && ((ini_get('apc.enabled') && SapiDetector::isWebScript()) || (ini_get('apc.enable_cli') && SapiDetector::isCliScript()));
44
    }
45
46
    /**
47
     * @return DriverStatistic
48
     */
49
    public function getStats(): DriverStatistic
50
    {
51
        $stats = (array)apcu_cache_info();
52
        $date = (new DateTime())->setTimestamp($stats['start_time']);
53
54
        return (new DriverStatistic())
55
            ->setData(implode(', ', array_keys($this->itemInstances)))
56
            ->setInfo(
57
                sprintf(
58
                    "The APCU cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.",
59
                    $date->format(DATE_RFC2822),
60
                    $stats['num_entries']
61
                )
62
            )
63
            ->setRawData($stats)
64
            ->setSize((int)$stats['mem_size']);
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    protected function driverConnect(): bool
71
    {
72
        return true;
73
    }
74
75
    /**
76
     * @param ExtendedCacheItemInterface $item
77
     * @return bool
78
     * @throws PhpfastcacheInvalidArgumentException
79
     */
80
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
81
    {
82
83
        return (bool)apcu_store($item->getKey(), $this->driverPreWrap($item), $item->getTtl());
84
    }
85
86
    /**
87
     * @param ExtendedCacheItemInterface $item
88
     * @return ?array<string, mixed>
89
     */
90
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
91
    {
92
        $data = apcu_fetch($item->getKey(), $success);
93
94
        if ($success === false || !\is_array($data)) {
95
            return null;
96
        }
97
98
        return $data;
99
    }
100
101
    /**
102
     * @param string $key
103
     * @param string $encodedKey
104
     * @return bool
105
     */
106
    protected function driverDelete(string $key, string $encodedKey): bool
107
    {
108
        return (bool)apcu_delete($key);
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    protected function driverClear(): bool
115
    {
116
        return @apcu_clear_cache();
117
    }
118
}
119