2222use Brainbits \Blocking \Owner \Owner ;
2323use DateTimeImmutable ;
2424use Psr \Clock \ClockInterface ;
25+ use Throwable ;
2526
2627use function assert ;
2728use function dirname ;
2829use function file_exists ;
2930use function file_get_contents ;
3031use function file_put_contents ;
31- use function is_array ;
3232use function is_string ;
3333use function is_writable ;
3434use function json_decode ;
@@ -137,13 +137,16 @@ public function exists(BlockIdentity $identity): bool
137137 $ metaContent = file_get_contents ($ metaFilename );
138138 assert (is_string ($ metaContent ));
139139 assert ($ metaContent !== '' );
140+ /** @var array{ttl: int, updatedAt: string} $metaData */
140141 $ metaData = json_decode ($ metaContent , true );
141- assert (is_array ($ metaData ));
142142
143143 $ now = $ this ->clock ->now ();
144-
145- $ expiresAt = (new DateTimeImmutable ((string ) $ metaData ['updatedAt ' ], $ now ->getTimezone ()))
146- ->modify ('+ ' . $ metaData ['ttl ' ] . ' seconds ' );
144+ try {
145+ $ expiresAt = (new DateTimeImmutable ((string ) $ metaData ['updatedAt ' ], $ now ->getTimezone ()))
146+ ->modify ('+ ' . $ metaData ['ttl ' ] . ' seconds ' );
147+ } catch (Throwable ) {
148+ throw UnserializeFailedException::createFromInput ($ metaContent );
149+ }
147150
148151 return $ expiresAt > $ now ;
149152 }
@@ -162,16 +165,19 @@ public function get(BlockIdentity $identity): Block|null
162165 throw UnserializeFailedException::createFromInput ($ content );
163166 }
164167
168+ /** @var array{identity: string, owner: string} $data */
165169 $ data = json_decode ($ content , true );
166170
167- assert (is_array ($ data ));
168- assert ($ data ['identity ' ] ?? false );
169- assert ($ data ['owner ' ] ?? false );
171+ try {
172+ $ block = new Block (
173+ new BlockIdentity ($ data ['identity ' ]),
174+ new Owner ($ data ['owner ' ]),
175+ );
176+ } catch (Throwable ) {
177+ throw UnserializeFailedException::createFromInput ($ content );
178+ }
170179
171- return new Block (
172- new BlockIdentity ($ data ['identity ' ]),
173- new Owner ($ data ['owner ' ]),
174- );
180+ return $ block ;
175181 }
176182
177183 private function getFilename (BlockIdentity $ identifier ): string
0 commit comments