8
8
use Shopware \Core \Checkout \Cart \Event \CartSavedEvent ;
9
9
use Shopware \Core \Checkout \Cart \Event \CartVerifyPersistEvent ;
10
10
use Shopware \Core \Defaults ;
11
- use Shopware \Core \Framework \Adapter \Cache \CacheValueCompressor ;
12
11
use Shopware \Core \Framework \DataAbstractionLayer \Doctrine \RetryableQuery ;
13
12
use Shopware \Core \Framework \Log \Package ;
14
13
use Shopware \Core \Framework \Plugin \Exception \DecorationPatternException ;
@@ -26,7 +25,7 @@ public function __construct(
26
25
private readonly Connection $ connection ,
27
26
private readonly EventDispatcherInterface $ eventDispatcher ,
28
27
private readonly CartSerializationCleaner $ cartSerializationCleaner ,
29
- private readonly bool $ compress
28
+ private readonly CartCompressor $ compressor
30
29
) {
31
30
}
32
31
@@ -47,7 +46,12 @@ public function load(string $token, SalesChannelContext $context): Cart
47
46
throw CartException::tokenNotFound ($ token );
48
47
}
49
48
50
- $ cart = $ content ['compressed ' ] ? CacheValueCompressor::uncompress ($ content ['payload ' ]) : unserialize ((string ) $ content ['payload ' ]);
49
+ try {
50
+ $ cart = $ this ->compressor ->unserialize ($ content ['payload ' ], (int ) $ content ['compressed ' ]);
51
+ } catch (\Exception ) {
52
+ // When we can't decode it, we have to delete it
53
+ throw CartException::tokenNotFound ($ token );
54
+ }
51
55
52
56
if (!$ cart instanceof Cart) {
53
57
throw CartException::deserializeFailed ();
@@ -87,12 +91,14 @@ public function save(Cart $cart, SalesChannelContext $context): void
87
91
ON DUPLICATE KEY UPDATE `payload` = :payload, `compressed` = :compressed, `rule_ids` = :rule_ids, `created_at` = :now;
88
92
SQL;
89
93
94
+ [$ compressed , $ serializeCart ] = $ this ->serializeCart ($ cart );
95
+
90
96
$ data = [
91
97
'token ' => $ cart ->getToken (),
92
- 'payload ' => $ this -> serializeCart ( $ cart ) ,
98
+ 'payload ' => $ serializeCart ,
93
99
'rule_ids ' => json_encode ($ context ->getRuleIds (), \JSON_THROW_ON_ERROR ),
94
100
'now ' => (new \DateTime ())->format (Defaults::STORAGE_DATE_TIME_FORMAT ),
95
- 'compressed ' => ( int ) $ this -> compress ,
101
+ 'compressed ' => $ compressed ,
96
102
];
97
103
98
104
$ query = new RetryableQuery ($ this ->connection , $ this ->connection ->prepare ($ sql ));
@@ -136,7 +142,10 @@ public function prune(int $days): void
136
142
} while ($ result > 0 );
137
143
}
138
144
139
- private function serializeCart (Cart $ cart ): string
145
+ /**
146
+ * @return array{0: int, 1: string}
147
+ */
148
+ private function serializeCart (Cart $ cart ): array
140
149
{
141
150
$ errors = $ cart ->getErrors ();
142
151
$ data = $ cart ->getData ();
@@ -146,7 +155,7 @@ private function serializeCart(Cart $cart): string
146
155
147
156
$ this ->cartSerializationCleaner ->cleanupCart ($ cart );
148
157
149
- $ serialized = $ this ->compress ? CacheValueCompressor:: compress ( $ cart ) : serialize ($ cart );
158
+ $ serialized = $ this ->compressor -> serialize ($ cart );
150
159
151
160
$ cart ->setErrors ($ errors );
152
161
$ cart ->setData ($ data );
0 commit comments