8
8
namespace Magento \Csp \Model ;
9
9
10
10
use Magento \Framework \App \CacheInterface ;
11
+ use Magento \Framework \App \ObjectManager ;
12
+ use Magento \Framework \Exception \FileSystemException ;
11
13
use Magento \Framework \Serialize \SerializerInterface ;
14
+ use Magento \Csp \Model \SubresourceIntegrity \Storage \File ;
12
15
13
16
/**
14
17
* Class contains methods equivalent to repository design to manage SRI hashes in cache.
@@ -47,22 +50,27 @@ class SubresourceIntegrityRepository
47
50
*/
48
51
private SubresourceIntegrityFactory $ integrityFactory ;
49
52
53
+ private File $ sriStorage ;
54
+
50
55
/**
51
56
* @param CacheInterface $cache
52
57
* @param SerializerInterface $serializer
53
58
* @param SubresourceIntegrityFactory $integrityFactory
54
59
* @param string|null $context
60
+ * @param File|null $sriStorage
55
61
*/
56
62
public function __construct (
57
63
CacheInterface $ cache ,
58
64
SerializerInterface $ serializer ,
59
65
SubresourceIntegrityFactory $ integrityFactory ,
60
- ?string $ context = null
66
+ ?string $ context = null ,
67
+ ? File $ sriStorage = null
61
68
) {
62
69
$ this ->cache = $ cache ;
63
70
$ this ->serializer = $ serializer ;
64
71
$ this ->integrityFactory = $ integrityFactory ;
65
72
$ this ->context = $ context ;
73
+ $ this ->sriStorage = $ sriStorage ?? ObjectManager::getInstance ()->get (File::class);
66
74
}
67
75
68
76
/**
@@ -94,6 +102,7 @@ public function getByPath(string $path): ?SubresourceIntegrity
94
102
* Gets all available Integrity objects.
95
103
*
96
104
* @return SubresourceIntegrity[]
105
+ * @throws FileSystemException
97
106
*/
98
107
public function getAll (): array
99
108
{
@@ -119,6 +128,7 @@ public function getAll(): array
119
128
* @param SubresourceIntegrity $integrity
120
129
*
121
130
* @return bool
131
+ * @throws FileSystemException
122
132
*/
123
133
public function save (SubresourceIntegrity $ integrity ): bool
124
134
{
@@ -128,10 +138,9 @@ public function save(SubresourceIntegrity $integrity): bool
128
138
129
139
$ this ->data = $ data ;
130
140
131
- return $ this ->cache ->save (
141
+ return $ this ->sriStorage ->save (
132
142
$ this ->serializer ->serialize ($ this ->data ),
133
- $ this ->getCacheKey (),
134
- [self ::CACHE_PREFIX ]
143
+ $ this ->context
135
144
);
136
145
}
137
146
@@ -152,36 +161,35 @@ public function saveBunch(array $bunch): bool
152
161
153
162
$ this ->data = $ data ;
154
163
155
- return $ this ->cache ->save (
164
+ return $ this ->sriStorage ->save (
156
165
$ this ->serializer ->serialize ($ this ->data ),
157
- $ this ->getCacheKey (),
158
- [self ::CACHE_PREFIX ]
166
+ $ this ->context
159
167
);
160
168
}
161
169
162
170
/**
163
171
* Deletes all Integrity objects.
164
172
*
165
173
* @return bool
174
+ * @throws FileSystemException
166
175
*/
167
176
public function deleteAll (): bool
168
177
{
169
178
$ this ->data = null ;
170
179
171
- return $ this ->cache ->remove (
172
- $ this ->getCacheKey ()
173
- );
180
+ return $ this ->sriStorage ->remove ();
174
181
}
175
182
176
183
/**
177
184
* Loads integrity data from a storage.
178
185
*
179
186
* @return array
187
+ * @throws FileSystemException
180
188
*/
181
189
private function getData (): array
182
190
{
183
191
if ($ this ->data === null ) {
184
- $ cache = $ this ->cache ->load ($ this ->getCacheKey () );
192
+ $ cache = $ this ->sriStorage ->load ($ this ->context );
185
193
186
194
$ this ->data = $ cache ? $ this ->serializer ->unserialize ($ cache ) : [];
187
195
}
@@ -193,6 +201,7 @@ private function getData(): array
193
201
* Gets a cache key based on current context.
194
202
*
195
203
* @return string
204
+ * @deprecated Filesystem storage used instead of a cache
196
205
*/
197
206
private function getCacheKey (): string
198
207
{
0 commit comments