1
|
|
|
<?php |
2
|
|
|
namespace Jsq\CacheEncryption\Iron; |
3
|
|
|
|
4
|
|
|
use Iron; |
5
|
|
|
use Iron\PasswordInterface; |
6
|
|
|
use Jsq\CacheEncryption\InvalidArgumentException; |
7
|
|
|
use Jsq\CacheEncryption\PoolDecorator as BasePoolDecorator; |
8
|
|
|
use Psr\Cache\CacheItemInterface; |
9
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
10
|
|
|
|
11
|
|
|
class PoolDecorator extends BasePoolDecorator |
12
|
|
|
{ |
13
|
|
|
/** @var CacheItemPoolInterface */ |
14
|
|
|
private $decorated; |
|
|
|
|
15
|
|
|
/** @var PasswordInterface */ |
16
|
|
|
private $password; |
17
|
|
|
/** @var string */ |
18
|
|
|
private $cipher; |
19
|
|
|
|
20
|
426 |
View Code Duplication |
public function __construct( |
|
|
|
|
21
|
|
|
CacheItemPoolInterface $decorated, |
22
|
|
|
$password, |
23
|
|
|
$cipher = Iron\Iron::DEFAULT_ENCRYPTION_METHOD |
24
|
|
|
) { |
25
|
426 |
|
if (!class_exists(Iron\Iron::class)) { |
26
|
|
|
// @codeCoverageIgnoreStart |
27
|
|
|
throw new \RuntimeException('You must install' |
28
|
|
|
. ' jsq/iron-php to use the Iron decorator.'); |
29
|
|
|
// @codeCoverageIgnoreEnd |
30
|
|
|
} |
31
|
|
|
|
32
|
426 |
|
parent::__construct($decorated); |
33
|
426 |
|
$this->decorated = $decorated; |
34
|
426 |
|
$this->password = Iron\normalize_password($password); |
35
|
426 |
|
$this->cipher = $cipher; |
36
|
426 |
|
} |
37
|
|
|
|
38
|
126 |
|
protected function decorate(CacheItemInterface $inner) |
39
|
|
|
{ |
40
|
126 |
|
return new ItemDecorator( |
41
|
|
|
$inner, |
42
|
126 |
|
$this->password, |
43
|
126 |
|
$this->cipher |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
75 |
|
public function save(CacheItemInterface $item) |
48
|
|
|
{ |
49
|
75 |
|
$this->finalizeItem($item); |
50
|
60 |
|
return $this->decorated->save($item->getDecorated()); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
42 |
|
public function saveDeferred(CacheItemInterface $item) |
54
|
|
|
{ |
55
|
42 |
|
$this->finalizeItem($item); |
56
|
27 |
|
return $this->decorated->saveDeferred($item->getDecorated()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
117 |
|
private function finalizeItem(CacheItemInterface $item) |
60
|
|
|
{ |
61
|
117 |
|
if ($item instanceof ItemDecorator) { |
62
|
87 |
|
return $item->finalize(); |
63
|
|
|
} |
64
|
|
|
|
65
|
30 |
|
throw new InvalidArgumentException('The provided cache item cannot' |
66
|
30 |
|
. ' be saved, as it did not originate from this cache.'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|