Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
18 / 19 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
| common_persistence_InMemoryKvDriver | |
94.74% |
18 / 19 |
|
87.50% |
7 / 8 |
13.02 | |
0.00% |
0 / 1 |
| connect | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| set | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| exists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| del | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| purge | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| incr | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| decr | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 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) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * Class common_persistence_InMemoryKvDriver |
| 25 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 26 | */ |
| 27 | class common_persistence_InMemoryKvDriver implements common_persistence_KvDriver, common_persistence_Purgable |
| 28 | { |
| 29 | /** |
| 30 | * @var array |
| 31 | */ |
| 32 | protected $persistence = []; |
| 33 | |
| 34 | /** |
| 35 | * |
| 36 | * @see common_persistence_Driver::connect() |
| 37 | */ |
| 38 | public function connect($id, array $params) |
| 39 | { |
| 40 | return new common_persistence_KeyValuePersistence($params, $this); |
| 41 | } |
| 42 | |
| 43 | public function set($id, $value, $ttl = null, $nx = false) |
| 44 | { |
| 45 | $this->persistence[$id] = $value; |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | public function get($id) |
| 50 | { |
| 51 | return $this->exists($id) ? $this->persistence[$id] : false; |
| 52 | } |
| 53 | |
| 54 | public function exists($id) |
| 55 | { |
| 56 | return array_key_exists($id, $this->persistence); |
| 57 | } |
| 58 | |
| 59 | public function del($id) |
| 60 | { |
| 61 | unset($this->persistence[$id]); |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | public function purge() |
| 66 | { |
| 67 | $this->persistence = []; |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | public function incr($id) |
| 72 | { |
| 73 | if (!isset($this->persistence[$id])) { |
| 74 | $this->persistence[$id] = 0; |
| 75 | } |
| 76 | if (!is_int($this->persistence[$id])) { |
| 77 | throw new common_exception_InconsistentData('Cannot increment non intvalue for ' . $id); |
| 78 | } |
| 79 | return ++$this->persistence[$id]; |
| 80 | } |
| 81 | |
| 82 | public function decr($id) |
| 83 | { |
| 84 | if (!isset($this->persistence[$id])) { |
| 85 | $this->persistence[$id] = 0; |
| 86 | } |
| 87 | if (!is_int($this->persistence[$id])) { |
| 88 | throw new common_exception_InconsistentData('Cannot decrement non intvalue for ' . $id); |
| 89 | } |
| 90 | return --$this->persistence[$id]; |
| 91 | } |
| 92 | } |