Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Session
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
8 / 8
18
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getItemTtl
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 saveItem
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getItem
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 hasItem
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 deleteItem
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 clear
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 destroy
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Cache\Adapter;
15
16/**
17 * Session adapter cache class
18 *
19 * @category   Pop
20 * @package    Pop\Cache
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.0.0
25 */
26class Session extends AbstractAdapter
27{
28
29    /**
30     * Constructor
31     *
32     * Instantiate the cache session object
33     *
34     * @param  int $ttl
35     */
36    public function __construct(int $ttl = 0)
37    {
38        parent::__construct($ttl);
39        if (session_id() == '') {
40            session_start();
41        }
42        if (!isset($_SESSION['_POP_CACHE_'])) {
43            $_SESSION['_POP_CACHE_'] = [];
44        }
45    }
46
47    /**
48     * Get the time-to-live for an item in cache
49     *
50     * @param  string $id
51     * @return int
52     */
53    public function getItemTtl(string $id): int
54    {
55        $ttl = 0;
56
57        if (isset($_SESSION['_POP_CACHE_'][$id])) {
58            $cacheValue = unserialize($_SESSION['_POP_CACHE_'][$id]);
59            $ttl        = $cacheValue['ttl'];
60        }
61
62        return $ttl;
63    }
64
65    /**
66     * Save an item to cache
67     *
68     * @param  string $id
69     * @param  mixed  $value
70     * @param  ?int   $ttl
71     * @return Session
72     */
73    public function saveItem(string $id, mixed $value, ?int $ttl = null): Session
74    {
75        $_SESSION['_POP_CACHE_'][$id] = serialize([
76            'start' => time(),
77            'ttl'   => ($ttl !== null) ? $ttl : $this->ttl,
78            'value' => $value
79        ]);
80        return $this;
81    }
82
83    /**
84     * Get an item from cache
85     *
86     * @param  string $id
87     * @return mixed
88     */
89    public function getItem(string $id): mixed
90    {
91        $value  = false;
92
93        if (isset($_SESSION['_POP_CACHE_'][$id])) {
94            $cacheValue = unserialize($_SESSION['_POP_CACHE_'][$id]);
95            if (($cacheValue['ttl'] == 0) || ((time() - $cacheValue['start']) <= $cacheValue['ttl'])) {
96                $value = $cacheValue['value'];
97            } else {
98                $this->deleteItem($id);
99            }
100        }
101
102        return $value;
103    }
104
105    /**
106     * Determine if the item exist in cache
107     *
108     * @param  string $id
109     * @return bool
110     */
111    public function hasItem(string $id): bool
112    {
113        $result = false;
114
115        if (isset($_SESSION['_POP_CACHE_'][$id])) {
116            $cacheValue = unserialize($_SESSION['_POP_CACHE_'][$id]);
117            $result = (($cacheValue['ttl'] == 0) || ((time() - $cacheValue['start']) <= $cacheValue['ttl']));
118        }
119
120        return $result;
121    }
122
123    /**
124     * Delete a value in cache
125     *
126     * @param  string $id
127     * @return Session
128     */
129    public function deleteItem(string $id): Session
130    {
131        if (isset($_SESSION['_POP_CACHE_'][$id])) {
132            unset($_SESSION['_POP_CACHE_'][$id]);
133        }
134        return $this;
135    }
136
137    /**
138     * Clear all stored values from cache
139     *
140     * @return Session
141     */
142    public function clear(): Session
143    {
144        $_SESSION['_POP_CACHE_'] = [];
145        return $this;
146    }
147
148    /**
149     * Destroy cache resource
150     *
151     * @return Session
152     */
153    public function destroy(): Session
154    {
155        $_SESSION = null;
156        session_unset();
157        session_destroy();
158        return $this;
159    }
160}