Passed
Push — 1.0.x ( 8353d5...3a2c37 )
by Julien
21:28
created

Cache::initializeCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Controller\Traits\Query;
13
14
use Phalcon\Filter\Exception;
15
use Phalcon\Filter\Filter;
16
use Phalcon\Support\Collection;
17
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractInjectable;
18
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractParams;
19
use Zemit\Mvc\Controller\Traits\Abstracts\Query\AbstractCache;
20
21
/**
22
 * This trait provides methods for caching data for the query.
23
 */
24
trait Cache
25
{
26
    use AbstractCache;
27
    
28
    use AbstractInjectable;
29
    use AbstractParams;
30
    
31
    public ?Collection $cache;
32
    public ?string $cacheKey;
33
    public ?int $cacheLifetime;
34
    
35
    /**
36
     * Initializes the cache.
37
     *
38
     * This method initializes the cache by setting the cache key and lifetime.
39
     *
40
     * @return void
41
     * @throws Exception
42
     */
43
    public function initializeCache(): void
44
    {
45
        $this->initializeCacheKey();
46
        $this->initializeCacheLifetime();
47
        
48
        $this->setCache(new Collection([
49
            'lifetime' => $this->getCacheLifetime(),
50
            'key' => $this->getCacheKey()
51
        ]));
52
    }
53
    
54
    /**
55
     * Initializes the cache lifetime.
56
     *
57
     * This method retrieves the 'lifetime' parameter using `getParam()` method,
58
     * applies the 'FILTER_ABSINT' filter to it, and then sets the cache lifetime
59
     * using `setCacheLifetime()` method with the filtered value.
60
     *
61
     * @return void
62
     * @throws Exception
63
     */
64
    public function initializeCacheLifetime(): void
65
    {
66
        $lifetime = $this->getParam('lifetime', [Filter::FILTER_ABSINT]);
67
        $this->setCacheLifetime($lifetime);
68
    }
69
    
70
    /**
71
     * Initializes the cache key based on the current parameters and user identity.
72
     *
73
     * This method generates a cache key by concatenating the user identity and a hash of the current parameters.
74
     * The generated cache key is then set as the value of the cache key for the current instance of the object.
75
     *
76
     * @return void
77
     */
78
    public function initializeCacheKey(): void
79
    {
80
        $paramsKey = hash('sha512', json_encode($this->getParams()));
81
        $identityKey = $this->identity->getUserId();
82
        $this->setCacheKey('_' . $identityKey . '-' . $paramsKey . '_');
83
    }
84
    
85
    /**
86
     * Sets the cache lifetime.
87
     *
88
     * @param int|null $cacheLifetime The cache lifetime.
89
     * @return void
90
     */
91
    public function setCacheLifetime(?int $cacheLifetime): void
92
    {
93
        $this->cacheLifetime = $cacheLifetime;
94
    }
95
    
96
    /**
97
     * Retrieves the cache lifetime.
98
     *
99
     * @return int|null The cache lifetime.
100
     */
101
    public function getCacheLifetime(): ?int
102
    {
103
        return $this->cacheLifetime;
104
    }
105
    
106
    /**
107
     * Sets the cache key.
108
     *
109
     * @param string|null $cacheKey The cache key.
110
     * @return void
111
     */
112
    public function setCacheKey(?string $cacheKey): void
113
    {
114
        $this->cacheKey = $cacheKey;
115
    }
116
    
117
    /**
118
     * Retrieves the cache key.
119
     *
120
     * @return string|null The cache key.
121
     */
122
    public function getCacheKey(): ?string
123
    {
124
        return $this->cacheKey;
125
    }
126
    
127
    /**
128
     * Set the cache collection for the query.
129
     *
130
     * @param Collection|null $cache The cache collection, or null to disable.
131
     * @return void
132
     */
133
    public function setCache(?Collection $cache): void
134
    {
135
        $this->cache = $cache;
136
    }
137
    
138
    /**
139
     * Retrieves the cache collection for the query.
140
     *
141
     * @return Collection|null The cache, or null if no cache is set.
142
     */
143
    public function getCache(): ?Collection
144
    {
145
        return $this->cache;
146
    }
147
}
148