Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
29 / 29 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| CacheItem | |
100.00% |
29 / 29 |
|
100.00% |
7 / 7 |
12 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isHit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| expiresAt | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| expiresAfter | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\oatbox\cache; |
| 24 | |
| 25 | use DateInterval; |
| 26 | use DateTime; |
| 27 | use DateTimeImmutable; |
| 28 | use DateTimeInterface; |
| 29 | use InvalidArgumentException; |
| 30 | use Psr\Cache\CacheItemInterface; |
| 31 | |
| 32 | class CacheItem implements CacheItemInterface |
| 33 | { |
| 34 | /** @var string */ |
| 35 | private $key; |
| 36 | |
| 37 | /** @var mixed */ |
| 38 | private $value; |
| 39 | |
| 40 | /** @var DateTime */ |
| 41 | private $expiry; |
| 42 | |
| 43 | /** @var bool */ |
| 44 | private $isHit; |
| 45 | |
| 46 | /** @var DateTime */ |
| 47 | private $currentDateTime; |
| 48 | |
| 49 | public function __construct(string $key, bool $hit = false, DateTimeInterface $currentDateTime = null) |
| 50 | { |
| 51 | $this->key = $key; |
| 52 | $this->isHit = $hit; |
| 53 | $this->currentDateTime = $currentDateTime ?? new DateTimeImmutable(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @inheritDoc |
| 58 | */ |
| 59 | public function getKey(): string |
| 60 | { |
| 61 | return $this->key; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @inheritDoc |
| 66 | */ |
| 67 | public function get() |
| 68 | { |
| 69 | return $this->value; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @inheritDoc |
| 74 | */ |
| 75 | public function isHit(): bool |
| 76 | { |
| 77 | return $this->isHit; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @inheritDoc |
| 82 | */ |
| 83 | public function set($value): self |
| 84 | { |
| 85 | $this->value = $value; |
| 86 | |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @inheritDoc |
| 92 | */ |
| 93 | public function expiresAt($expiration): self |
| 94 | { |
| 95 | if (null === $expiration) { |
| 96 | $this->expiry = null; |
| 97 | |
| 98 | return $this; |
| 99 | } |
| 100 | |
| 101 | if ($expiration instanceof DateTimeInterface) { |
| 102 | $this->expiry = $expiration->getTimestamp(); |
| 103 | |
| 104 | return $this; |
| 105 | } |
| 106 | |
| 107 | throw new InvalidArgumentException('Expiration date must implement DateTimeInterface or be null'); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @inheritDoc |
| 112 | * |
| 113 | * @param DateInterval|null $time |
| 114 | */ |
| 115 | public function expiresAfter($time): self |
| 116 | { |
| 117 | if (null === $time) { |
| 118 | $this->expiry = null; |
| 119 | |
| 120 | return $this; |
| 121 | } |
| 122 | |
| 123 | if (is_int($time)) { |
| 124 | $time = new DateInterval(sprintf('PT%dS', $time)); |
| 125 | } |
| 126 | |
| 127 | if ($time instanceof DateInterval) { |
| 128 | $this->expiry = $this->currentDateTime->add($time)->getTimestamp(); |
| 129 | |
| 130 | return $this; |
| 131 | } |
| 132 | |
| 133 | throw new InvalidArgumentException( |
| 134 | sprintf( |
| 135 | 'Expiration date must implement DateTimeInterface or be null, "%s" given.', |
| 136 | gettype($time) |
| 137 | ) |
| 138 | ); |
| 139 | } |
| 140 | } |