Skip to content

Commit a1b7803

Browse files
Stephan Wentzpl-github
authored andcommitted
handle failed unserialize
1 parent 6e1ca90 commit a1b7803

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
/*
6+
* This file is part of the brainbits blocking package.
7+
*
8+
* (c) brainbits GmbH (http://www.brainbits.net)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Brainbits\Blocking\Exception;
15+
16+
/**
17+
* Unserialize failed exception.
18+
*/
19+
final class UnserializeFailedException extends RuntimeException
20+
{
21+
/**
22+
* @param mixed $input
23+
*/
24+
public static function createFromInput($input): self
25+
{
26+
return new self("Unserialize failed: ".$input);
27+
}
28+
}

src/Storage/FilesystemStorage.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Brainbits\Blocking\Exception\DirectoryNotWritableException;
1818
use Brainbits\Blocking\Exception\FileNotWritableException;
1919
use Brainbits\Blocking\Exception\IOException;
20+
use Brainbits\Blocking\Exception\UnserializeFailedException;
2021
use Brainbits\Blocking\Identity\IdentityInterface;
2122
use DateTimeImmutable;
2223

@@ -92,6 +93,10 @@ public function get(IdentityInterface $identifier): ?BlockInterface
9293
$content = file_get_contents($filename);
9394
$updatedAt = DateTimeImmutable::createFromFormat('U', (string) filemtime($filename));
9495
$block = unserialize($content);
96+
if (!$block) {
97+
throw UnserializeFailedException::createFromInput($content);
98+
}
99+
95100
$block->touch($updatedAt);
96101

97102
return $block;

0 commit comments

Comments
 (0)