CacheerHelper::recoverFromStorage()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 1
b 0
f 0
nc 6
nop 3
dl 0
loc 20
rs 9.2222
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Helpers;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
8
class CacheerHelper
9
{
10
    
11
    /**
12
     * Validates a cache item to ensure it contains the required keys.
13
     * 
14
     * @param array $item
15
     * @param callable|null $exceptionFactory
16
     * @return void
17
     */
18
    public static function validateCacheItem(array $item, ?callable $exceptionFactory = null)
19
    {
20
        if (!isset($item['cacheKey']) || !isset($item['cacheData'])) {
21
            if ($exceptionFactory) {
22
                throw $exceptionFactory("Each item must contain 'cacheKey' and 'cacheData'");
23
            }
24
            throw new InvalidArgumentException("Each item must contain 'cacheKey' and 'cacheData'");
25
        }
26
    }
27
28
    /**
29
     * Merges cache data with existing data.
30
     * 
31
     * @param mixed $cacheData
32
     * @return array
33
     */
34
    public static function mergeCacheData($cacheData)
35
    {
36
        if (is_array($cacheData) && is_array(reset($cacheData))) {
37
            $merged = [];
38
            foreach ($cacheData as $data) {
39
                $merged[] = $data;
40
            }
41
            return $merged;
42
        }
43
        return (array)$cacheData;
44
    }
45
46
    /**
47
     * Generates an array identifier for cache data.
48
     * 
49
     * @param mixed $currentCacheData
50
     * @param mixed $cacheData
51
     * @return array
52
     */
53
    public static function arrayIdentifier(mixed $currentCacheData, mixed $cacheData)
54
    {
55
        if (is_array($currentCacheData) && is_array($cacheData)) {
56
            return array_merge($currentCacheData, $cacheData);
57
        }
58
        return array_merge((array)$currentCacheData, (array)$cacheData);
59
    }
60
61
    /**
62
     * Prepares data for storage, applying compression and/or encryption.
63
     * 
64
     * @param mixed $data
65
     * @param bool $compression
66
     * @param string|null $encryptionKey
67
     * @return mixed
68
     */
69
    public static function prepareForStorage(mixed $data, bool $compression = false, ?string $encryptionKey = null)
70
    {
71
        if (!$compression && is_null($encryptionKey)) {
72
            return $data;
73
        }
74
75
        $payload = serialize($data);
76
77
        if ($compression) {
78
            $payload = gzcompress($payload);
79
        }
80
81
        if (!is_null($encryptionKey)) {
82
            $iv = substr(hash('sha256', $encryptionKey), 0, 16);
83
            $encrypted = openssl_encrypt($payload, 'AES-256-CBC', $encryptionKey, 0, $iv);
84
            if ($encrypted === false) {
85
                throw new RuntimeException('Failed to encrypt cache data');
86
            }
87
            $payload = $encrypted;
88
        }
89
90
        return $payload;
91
    }
92
93
    /**
94
     * Recovers data from storage, applying decompression and/or decryption.
95
     * 
96
     * @param mixed $data
97
     * @param bool $compression
98
     * @param string|null $encryptionKey
99
     * @return mixed
100
     */
101
    public static function recoverFromStorage(mixed $data, bool $compression = false, ?string $encryptionKey = null)
102
    {
103
        if (!$compression && is_null($encryptionKey)) {
104
            return $data;
105
        }
106
107
        if (!is_null($encryptionKey)) {
108
            $iv = substr(hash('sha256', $encryptionKey), 0, 16);
109
            $decrypted = openssl_decrypt($data, 'AES-256-CBC', $encryptionKey, 0, $iv);
110
            if ($decrypted === false) {
111
                throw new RuntimeException('Failed to decrypt cache data');
112
            }
113
            $data = $decrypted;
114
        }
115
116
        if ($compression) {
117
            $data = gzuncompress($data);
118
        }
119
120
        return unserialize($data);
121
    }
122
}