Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractAdapter
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setTtl
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTtl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isFresh
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getItemTtl
n/a
0 / 0
n/a
0 / 0
0
 saveItem
n/a
0 / 0
n/a
0 / 0
0
 getItem
n/a
0 / 0
n/a
0 / 0
0
 hasItem
n/a
0 / 0
n/a
0 / 0
0
 deleteItem
n/a
0 / 0
n/a
0 / 0
0
 clear
n/a
0 / 0
n/a
0 / 0
0
 destroy
n/a
0 / 0
n/a
0 / 0
0
 incrementItem
n/a
0 / 0
n/a
0 / 0
0
 decrementItem
n/a
0 / 0
n/a
0 / 0
0
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Cache\Adapter;
16
17use Pop\Cache\Clock;
18
19/**
20 * Cache adapter abstract class
21 *
22 * @category   Pop
23 * @package    Pop\Cache
24 * @author     Nick Sagona, III <nick@popphp.org>
25 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    5.0.0
28 */
29abstract class AbstractAdapter implements AdapterInterface
30{
31
32    /**
33     * Global time-to-live
34     * @var int
35     */
36    protected int $ttl = 0;
37
38    /**
39     * Clock used to resolve the current time
40     * @var Clock\ClockInterface
41     */
42    protected Clock\ClockInterface $clock;
43
44    /**
45     * Constructor
46     *
47     * Instantiate the cache adapter object
48     *
49     * @param  int                  $ttl
50     * @param  Clock\ClockInterface $clock
51     */
52    public function __construct(int $ttl = 0, Clock\ClockInterface $clock = new Clock\SystemClock())
53    {
54        $this->setTtl($ttl);
55        $this->clock = $clock;
56    }
57
58    /**
59     * Set the global time-to-live for the cache adapter
60     *
61     * @param  int $ttl
62     * @return AbstractAdapter
63     */
64    public function setTtl(int $ttl): AbstractAdapter
65    {
66        $this->ttl = $ttl;
67        return $this;
68    }
69
70    /**
71     * Get the global time-to-live for the cache object
72     *
73     * @return int
74     */
75    public function getTtl(): int
76    {
77        return $this->ttl;
78    }
79
80    /**
81     * Determine if a cache envelope (with 'start'/'ttl' keys) is still within its time-to-live
82     *
83     * @param  array $envelope
84     * @return bool
85     */
86    protected function isFresh(array $envelope): bool
87    {
88        return ($envelope['ttl'] == 0) || (($this->clock->now() - $envelope['start']) <= $envelope['ttl']);
89    }
90
91    /**
92     * Get the time-to-live for an item in cache
93     *
94     * @param  string $id
95     * @param  int    $default
96     * @return int
97     */
98    abstract public function getItemTtl(string $id, int $default = 0): int;
99
100    /**
101     * Save an item to cache
102     *
103     * @param  string $id
104     * @param  mixed  $value
105     * @param  ?int   $ttl
106     * @return AbstractAdapter
107     */
108    abstract public function saveItem(string $id, mixed $value, ?int $ttl = null): AbstractAdapter;
109
110    /**
111     * Get an item from cache
112     *
113     * @param  string $id
114     * @param  mixed  $default
115     * @return mixed
116     */
117    abstract public function getItem(string $id, mixed $default = false): mixed;
118
119    /**
120     * Determine if the item exist in cache
121     *
122     * @param  string $id
123     * @return bool
124     */
125    abstract public function hasItem(string $id): bool;
126
127    /**
128     * Delete a value in cache
129     *
130     * @param  string $id
131     * @return AbstractAdapter
132     */
133    abstract public function deleteItem(string $id): AbstractAdapter;
134
135    /**
136     * Clear all stored values from cache
137     *
138     * @return AbstractAdapter
139     */
140    abstract public function clear(): AbstractAdapter;
141
142    /**
143     * Destroy cache resource
144     *
145     * @return AbstractAdapter
146     */
147    abstract public function destroy(): AbstractAdapter;
148
149    /**
150     * Atomically increment a counter in cache, creating it at $initial if it doesn't exist
151     *
152     * @param  string $id
153     * @param  int    $amount
154     * @param  int    $initial
155     * @param  ?int   $ttl
156     * @return int
157     */
158    abstract public function incrementItem(string $id, int $amount = 1, int $initial = 0, ?int $ttl = null): int;
159
160    /**
161     * Atomically decrement a counter in cache, creating it at $initial if it doesn't exist
162     *
163     * @param  string $id
164     * @param  int    $amount
165     * @param  int    $initial
166     * @param  ?int   $ttl
167     * @return int
168     */
169    abstract public function decrementItem(string $id, int $amount = 1, int $initial = 0, ?int $ttl = null): int;
170
171}