1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2024 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
8
8
namespace Magento \Csp \Model ;
9
9
10
+ use Magento \Framework \App \ObjectManager ;
10
11
use Magento \Framework \App \CacheInterface ;
11
12
use Magento \Framework \Serialize \SerializerInterface ;
13
+ use Magento \Csp \Model \SubresourceIntegrity \StorageInterface ;
12
14
13
15
/**
14
- * Class contains methods equivalent to repository design to manage SRI hashes in cache .
16
+ * Class contains methods equivalent to repository design to manage SRI hashes.
15
17
*/
16
18
class SubresourceIntegrityRepository
17
19
{
18
- /**
19
- * Cache prefix.
20
- *
21
- * @var string
22
- */
23
- private const CACHE_PREFIX = 'INTEGRITY ' ;
24
-
25
20
/**
26
21
* @var array|null
27
22
*/
@@ -32,37 +27,38 @@ class SubresourceIntegrityRepository
32
27
*/
33
28
private ?string $ context ;
34
29
35
- /**
36
- * @var CacheInterface
37
- */
38
- private CacheInterface $ cache ;
39
-
40
30
/**
41
31
* @var SerializerInterface
42
32
*/
43
33
private SerializerInterface $ serializer ;
44
34
45
35
/**
46
- * @var SubresourceIntegrityFactory
36
+ * @var StorageInterface
47
37
*/
48
- private SubresourceIntegrityFactory $ integrityFactory ;
38
+ private StorageInterface $ storage ;
49
39
50
40
/**
51
41
* @param CacheInterface $cache
52
42
* @param SerializerInterface $serializer
53
43
* @param SubresourceIntegrityFactory $integrityFactory
54
44
* @param string|null $context
45
+ * @param StorageInterface|null $storage
46
+ *
47
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
55
48
*/
56
49
public function __construct (
57
50
CacheInterface $ cache ,
58
51
SerializerInterface $ serializer ,
59
52
SubresourceIntegrityFactory $ integrityFactory ,
60
- ?string $ context = null
53
+ ?string $ context = null ,
54
+ ?StorageInterface $ storage = null
61
55
) {
62
- $ this ->cache = $ cache ;
63
56
$ this ->serializer = $ serializer ;
64
- $ this ->integrityFactory = $ integrityFactory ;
65
57
$ this ->context = $ context ;
58
+
59
+ $ this ->storage = $ storage ?? ObjectManager::getInstance ()->get (
60
+ StorageInterface::class
61
+ );
66
62
}
67
63
68
64
/**
@@ -74,20 +70,7 @@ public function __construct(
74
70
*/
75
71
public function getByPath (string $ path ): ?SubresourceIntegrity
76
72
{
77
- $ data = $ this ->getData ();
78
-
79
- if (isset ($ data [$ path ])) {
80
- return $ this ->integrityFactory ->create (
81
- [
82
- "data " => [
83
- "path " => $ path ,
84
- "hash " => $ data [$ path ]
85
- ]
86
- ]
87
- );
88
- }
89
-
90
- return null ;
73
+ return $ this ->getData ()[$ path ] ?? null ;
91
74
}
92
75
93
76
/**
@@ -97,20 +80,7 @@ public function getByPath(string $path): ?SubresourceIntegrity
97
80
*/
98
81
public function getAll (): array
99
82
{
100
- $ result = [];
101
-
102
- foreach ($ this ->getData () as $ path => $ hash ) {
103
- $ result [] = $ this ->integrityFactory ->create (
104
- [
105
- "data " => [
106
- "path " => $ path ,
107
- "hash " => $ hash
108
- ]
109
- ]
110
- );
111
- }
112
-
113
- return $ result ;
83
+ return array_values ($ this ->getData ());
114
84
}
115
85
116
86
/**
@@ -124,14 +94,16 @@ public function save(SubresourceIntegrity $integrity): bool
124
94
{
125
95
$ data = $ this ->getData ();
126
96
127
- $ data [$ integrity ->getPath ()] = $ integrity-> getHash () ;
97
+ $ data [$ integrity ->getPath ()] = $ integrity ;
128
98
129
99
$ this ->data = $ data ;
130
100
131
- return $ this ->cache ->save (
132
- $ this ->serializer ->serialize ($ this ->data ),
133
- $ this ->getCacheKey (),
134
- [self ::CACHE_PREFIX ]
101
+ // Transform the data before saving.
102
+ $ transformedData = array_map (fn ($ integrity ) => $ integrity ->getHash (), $ this ->data );
103
+
104
+ return $ this ->storage ->save (
105
+ $ this ->serializer ->serialize ($ transformedData ),
106
+ $ this ->context
135
107
);
136
108
}
137
109
@@ -147,15 +119,17 @@ public function saveBunch(array $bunch): bool
147
119
$ data = $ this ->getData ();
148
120
149
121
foreach ($ bunch as $ integrity ) {
150
- $ data [$ integrity ->getPath ()] = $ integrity-> getHash () ;
122
+ $ data [$ integrity ->getPath ()] = $ integrity ;
151
123
}
152
124
153
125
$ this ->data = $ data ;
154
126
155
- return $ this ->cache ->save (
156
- $ this ->serializer ->serialize ($ this ->data ),
157
- $ this ->getCacheKey (),
158
- [self ::CACHE_PREFIX ]
127
+ // Transform the data before saving.
128
+ $ transformedData = array_map (fn ($ integrity ) => $ integrity ->getHash (), $ this ->data );
129
+
130
+ return $ this ->storage ->save (
131
+ $ this ->serializer ->serialize ($ transformedData ),
132
+ $ this ->context
159
133
);
160
134
}
161
135
@@ -168,9 +142,7 @@ public function deleteAll(): bool
168
142
{
169
143
$ this ->data = null ;
170
144
171
- return $ this ->cache ->remove (
172
- $ this ->getCacheKey ()
173
- );
145
+ return $ this ->storage ->remove ($ this ->context );
174
146
}
175
147
176
148
/**
@@ -181,27 +153,15 @@ public function deleteAll(): bool
181
153
private function getData (): array
182
154
{
183
155
if ($ this ->data === null ) {
184
- $ cache = $ this ->cache ->load ($ this ->getCacheKey ());
185
-
186
- $ this ->data = $ cache ? $ this ->serializer ->unserialize ($ cache ) : [];
187
- }
188
-
189
- return $ this ->data ;
190
- }
156
+ $ rawData = $ this ->storage ->load ($ this ->context );
191
157
192
- /**
193
- * Gets a cache key based on current context.
194
- *
195
- * @return string
196
- */
197
- private function getCacheKey (): string
198
- {
199
- $ cacheKey = self ::CACHE_PREFIX ;
158
+ $ this ->data = $ rawData ? $ this ->serializer ->unserialize ($ rawData ) : [];
200
159
201
- if ($ this ->context ) {
202
- $ cacheKey .= "_ " . $ this ->context ;
160
+ foreach ($ this ->data as $ path => $ hash ) {
161
+ $ this ->data [$ path ] = new SubresourceIntegrity (["path " => $ path , "hash " => $ hash ]);
162
+ }
203
163
}
204
164
205
- return $ cacheKey ;
165
+ return $ this -> data ;
206
166
}
207
167
}
0 commit comments