7
7
8
8
namespace Magento \Csp \Model ;
9
9
10
- use Psr \Log \LoggerInterface ;
11
10
use Magento \Framework \App \CacheInterface ;
12
- use Magento \Framework \App \DeploymentConfig ;
13
- use Magento \Framework \Exception \FileSystemException ;
14
- use Magento \Framework \Exception \RuntimeException ;
15
11
use Magento \Framework \Serialize \SerializerInterface ;
16
12
17
13
/**
18
- * Class contains methods equivalent to repository design to manage SRI hashes in cache
14
+ * Class contains methods equivalent to repository design to manage SRI hashes in cache.
19
15
*/
20
16
class SubresourceIntegrityRepository
21
17
{
@@ -24,22 +20,17 @@ class SubresourceIntegrityRepository
24
20
*
25
21
* @var string
26
22
*/
27
- private const CACHE_PREFIX = 'INTEGRITY_HASH ' ;
23
+ private const CACHE_PREFIX = 'INTEGRITY ' ;
28
24
29
25
/**
30
- * @var CacheInterface
31
- */
32
- private CacheInterface $ cache ;
33
-
34
- /**
35
- * @var LoggerInterface
26
+ * @var array|null
36
27
*/
37
- private LoggerInterface $ logger ;
28
+ private ? array $ data = null ;
38
29
39
30
/**
40
- * @var DeploymentConfig
31
+ * @var CacheInterface
41
32
*/
42
- private DeploymentConfig $ config ;
33
+ private CacheInterface $ cache ;
43
34
44
35
/**
45
36
* @var SerializerInterface
@@ -52,24 +43,16 @@ class SubresourceIntegrityRepository
52
43
private SubresourceIntegrityFactory $ integrityFactory ;
53
44
54
45
/**
55
- * constructor
56
- *
57
46
* @param CacheInterface $cache
58
- * @param LoggerInterface $logger
59
- * @param DeploymentConfig $config
60
47
* @param SerializerInterface $serializer
61
48
* @param SubresourceIntegrityFactory $integrityFactory
62
49
*/
63
50
public function __construct (
64
51
CacheInterface $ cache ,
65
- LoggerInterface $ logger ,
66
- DeploymentConfig $ config ,
67
52
SerializerInterface $ serializer ,
68
53
SubresourceIntegrityFactory $ integrityFactory
69
54
) {
70
55
$ this ->cache = $ cache ;
71
- $ this ->logger = $ logger ;
72
- $ this ->config = $ config ;
73
56
$ this ->serializer = $ serializer ;
74
57
$ this ->integrityFactory = $ integrityFactory ;
75
58
}
@@ -83,17 +66,15 @@ public function __construct(
83
66
*/
84
67
public function getByUrl (string $ url ): ?SubresourceIntegrity
85
68
{
86
- $ integrity = $ this ->cache ->load (
87
- self ::CACHE_PREFIX . $ url
88
- );
69
+ $ data = $ this ->getData ();
89
70
90
- if (!$ integrity ) {
91
- return null ;
71
+ if (isset ($ data [$ url ])) {
72
+ return $ this ->integrityFactory ->create (
73
+ ["data " => $ data [$ url ]]
74
+ );
92
75
}
93
76
94
- return $ this ->integrityFactory ->create (
95
- ["data " => $ this ->serializer ->unserialize ($ integrity )]
96
- );
77
+ return null ;
97
78
}
98
79
99
80
/**
@@ -105,29 +86,12 @@ public function getAll(): array
105
86
{
106
87
$ result = [];
107
88
108
- try {
109
- $ defaultCachePrefix = $ this ->config ->get (
110
- "cache/frontend/default/id_prefix "
89
+ foreach ($ this ->getData () as $ integrity ) {
90
+ $ result [] = $ this ->integrityFactory ->create (
91
+ [
92
+ "data " => $ integrity
93
+ ]
111
94
);
112
-
113
- $ cacheIds = $ this ->cache ->getFrontend ()->getLowLevelFrontend ()
114
- ->getIdsMatchingAnyTags (
115
- [$ defaultCachePrefix . self ::CACHE_PREFIX ]
116
- );
117
-
118
- foreach ($ cacheIds as $ id ) {
119
- $ integrity = $ this ->cache ->load ($ id );
120
-
121
- if ($ integrity ) {
122
- $ result [] = $ this ->integrityFactory ->create (
123
- [
124
- "data " => $ this ->serializer ->unserialize ($ integrity )
125
- ]
126
- );
127
- }
128
- }
129
- } catch (\Exception $ e ) {
130
- $ this ->logger ->critical ($ e );
131
95
}
132
96
133
97
return $ result ;
@@ -142,24 +106,35 @@ public function getAll(): array
142
106
*/
143
107
public function save (SubresourceIntegrity $ integrity ): bool
144
108
{
109
+ $ data = $ this ->getData ();
110
+
111
+ $ data [$ integrity ->getUrl ()] = [
112
+ "url " => $ integrity ->getUrl (),
113
+ "hash " => $ integrity ->getHash ()
114
+ ];
115
+
116
+ $ this ->data = $ data ;
117
+
145
118
return $ this ->cache ->save (
146
- $ this ->serializer ->serialize ($ integrity -> getData () ),
147
- self ::CACHE_PREFIX . $ integrity -> getUrl () ,
119
+ $ this ->serializer ->serialize ($ this -> data ),
120
+ self ::CACHE_PREFIX ,
148
121
[self ::CACHE_PREFIX ]
149
122
);
150
123
}
151
124
152
125
/**
153
- * Clear contents of cache
154
- *
155
- * @param string $url
126
+ * Loads integrity data from a storage.
156
127
*
157
- * @return bool
128
+ * @return array
158
129
*/
159
- public function deleteByUrl ( string $ url ): bool
130
+ private function getData ( ): array
160
131
{
161
- return $ this ->cache ->clean (
162
- [self ::CACHE_PREFIX . $ url ]
163
- );
132
+ if ($ this ->data === null ) {
133
+ $ cache = $ this ->cache ->load (self ::CACHE_PREFIX );
134
+
135
+ $ this ->data = $ cache ? $ this ->serializer ->unserialize ($ cache ) : [];
136
+ }
137
+
138
+ return $ this ->data ;
164
139
}
165
140
}
0 commit comments