|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of php-cache organization. |
| 5 | + * |
| 6 | + * (c) 2015-2016 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
| 7 | + * |
| 8 | + * This source file is subject to the MIT license that is bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + */ |
| 11 | + |
| 12 | + |
| 13 | +namespace Cache\Encryption; |
| 14 | + |
| 15 | +use Cache\Adapter\Common\HasExpirationDateInterface; |
| 16 | +use Cache\Taggable\TaggableItemInterface; |
| 17 | +use Defuse\Crypto\Crypto; |
| 18 | +use Defuse\Crypto\Key; |
| 19 | +use Psr\Cache\CacheItemInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Encrypt and Decrypt all the stored items. |
| 23 | + * |
| 24 | + * @author Daniel Bannert <[email protected]> |
| 25 | + */ |
| 26 | +class EncryptedItemDecorator implements CacheItemInterface, HasExpirationDateInterface, TaggableItemInterface |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @type CacheItemInterface |
| 30 | + */ |
| 31 | + private $cacheItem; |
| 32 | + |
| 33 | + /** |
| 34 | + * @type Key |
| 35 | + */ |
| 36 | + private $key; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param CacheItemInterface $cacheItem |
| 40 | + * @param Key $key |
| 41 | + */ |
| 42 | + public function __construct(CacheItemInterface $cacheItem, Key $key) |
| 43 | + { |
| 44 | + $this->cacheItem = $cacheItem; |
| 45 | + $this->key = $key; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritdoc} |
| 50 | + */ |
| 51 | + public function getKey() |
| 52 | + { |
| 53 | + return $this->cacheItem->getKey(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritdoc} |
| 58 | + */ |
| 59 | + public function set($value) |
| 60 | + { |
| 61 | + $type = gettype($value); |
| 62 | + |
| 63 | + if ($type === 'object') { |
| 64 | + $value = serialize($value); |
| 65 | + } |
| 66 | + |
| 67 | + $json = json_encode(['type' => $type, 'value' => $value]); |
| 68 | + |
| 69 | + $this->cacheItem->set(Crypto::encrypt($json, $this->key)); |
| 70 | + |
| 71 | + return $this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * {@inheritdoc} |
| 76 | + */ |
| 77 | + public function get() |
| 78 | + { |
| 79 | + if (!$this->isHit()) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + $item = json_decode(Crypto::decrypt($this->cacheItem->get(), $this->key), true); |
| 84 | + |
| 85 | + return $this->transform($item); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * {@inheritdoc} |
| 90 | + */ |
| 91 | + public function isHit() |
| 92 | + { |
| 93 | + return $this->cacheItem->isHit(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * {@inheritdoc} |
| 98 | + */ |
| 99 | + public function getExpirationDate() |
| 100 | + { |
| 101 | + return $this->cacheItem->getExpirationDate(); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * {@inheritdoc} |
| 106 | + */ |
| 107 | + public function expiresAt($expiration) |
| 108 | + { |
| 109 | + $this->cacheItem->expiresAt($expiration); |
| 110 | + |
| 111 | + return $this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * {@inheritdoc} |
| 116 | + */ |
| 117 | + public function expiresAfter($time) |
| 118 | + { |
| 119 | + $this->cacheItem->expiresAfter($time); |
| 120 | + |
| 121 | + return $this; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * {@inheritdoc} |
| 126 | + */ |
| 127 | + public function getTags() |
| 128 | + { |
| 129 | + return $this->cacheItem->getTags(); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * {@inheritdoc} |
| 134 | + */ |
| 135 | + public function addTag($tag) |
| 136 | + { |
| 137 | + $this->cacheItem->addTag($tag); |
| 138 | + |
| 139 | + return $this; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * {@inheritdoc} |
| 144 | + */ |
| 145 | + public function setTags(array $tags) |
| 146 | + { |
| 147 | + $this->cacheItem->setTags($tags); |
| 148 | + |
| 149 | + return $this; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Creating a copy of the orginal CacheItemInterface object. |
| 154 | + */ |
| 155 | + public function __clone() |
| 156 | + { |
| 157 | + $this->cacheItem = clone $this->cacheItem; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Transfrom value back to it orginal type. |
| 162 | + * |
| 163 | + * @param array $item |
| 164 | + * |
| 165 | + * @return mixed |
| 166 | + */ |
| 167 | + private function transform(array $item) |
| 168 | + { |
| 169 | + if ($item['type'] === 'object') { |
| 170 | + return unserialize($item['value']); |
| 171 | + } |
| 172 | + |
| 173 | + $value = $item['value']; |
| 174 | + |
| 175 | + settype($value, $item['type']); |
| 176 | + |
| 177 | + return $value; |
| 178 | + } |
| 179 | +} |
0 commit comments