Skip to content

Commit

Permalink
using exception
Browse files Browse the repository at this point in the history
  • Loading branch information
kierwils committed Mar 8, 2022
1 parent a134898 commit d173374
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
}
],
"provide": {
"psr/cache-implementation": "1.0",
"psr/simple-cache-implementation": "1.0"
"psr/cache-implementation": "1.0"
},
"require": {
"php": ">=7.4",
Expand Down
18 changes: 14 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

Example

use Stash\{
use Codin\Stash\{
Item,
RedisPool
Adapter\RedisPool
Adapter\Redis\ConnectionResolver
};

$redis = new Redis;
$pool = new RedisPool($redis);
$resolver = new ConnectionResolver(static function () {
return new Redis();
});
$pool = new RedisPool($resolver);

$item = new Item('my-key', 'some data');
$item->expiresAfter(3600); // 1 hour
Expand All @@ -18,3 +21,10 @@ Example
$item = $pool->getItem('my-key');
echo $item->get(); // some data
echo $item->isHit(); // true

## Testing

```
php bin/phpstan
php bin/phpspec run
```
3 changes: 2 additions & 1 deletion spec/Codin/Stash/Adapter/FileSystem/PhpSerialiserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace spec\Codin\Stash\Adapter\FileSystem;

use Codin\Stash\Exceptions\SerialisationError;
use Codin\Stash\Item;
use PhpSpec\ObjectBehavior;

Expand Down Expand Up @@ -33,6 +34,6 @@ public function it_should_catch_decoded_unknowns()

$encoded = serialize($item);

$this->shouldThrow(\RuntimeException::class)->duringDecode($encoded);
$this->shouldThrow(SerialisationError::class)->duringDecode($encoded);
}
}
3 changes: 2 additions & 1 deletion src/Adapter/FileSystem/PhpSerialiser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Codin\Stash\Adapter\FileSystem;

use Codin\Stash\Exceptions;
use Psr\Cache\CacheItemInterface;

class PhpSerialiser implements Serialiser
Expand All @@ -18,7 +19,7 @@ public function decode(string $data): CacheItemInterface
$object = unserialize($data);

if (!$object instanceof CacheItemInterface) {
throw new \RuntimeException('Failed to decode data for unknown object');
throw new Exceptions\SerialisationError('Failed to decode data for unknown object');
}

return $object;
Expand Down
12 changes: 12 additions & 0 deletions src/Exceptions/SerialisationError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Codin\Stash\Exceptions;

use Exception;
use Psr\Cache\CacheException;

class SerialisationError extends Exception implements CacheException
{
}

0 comments on commit d173374

Please sign in to comment.