Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
14.29% |
2 / 14 |
|
25.00% |
2 / 8 |
CRAP | |
0.00% |
0 / 1 |
| common_session_php_KeyValueSessionHandler | |
14.29% |
2 / 14 |
|
25.00% |
2 / 8 |
72.97 | |
0.00% |
0 / 1 |
| getPersistence | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| open | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| close | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| read | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| write | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| destroy | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| gc | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSessionTtl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | use oat\oatbox\service\ConfigurableService; |
| 23 | |
| 24 | /** |
| 25 | * Session implementation as a Key Value storage and using the persistence |
| 26 | * |
| 27 | * @author Joel Bout <joel@taotesting.com> |
| 28 | * @package generis |
| 29 | */ |
| 30 | class common_session_php_KeyValueSessionHandler extends ConfigurableService implements common_session_php_SessionHandler |
| 31 | { |
| 32 | public const OPTION_PERSISTENCE = 'persistence'; |
| 33 | public const OPTION_SESSION_TTL = 'session_ttl'; |
| 34 | |
| 35 | public const KEY_NAMESPACE = 'generis:session:'; |
| 36 | |
| 37 | /** @var common_persistence_KeyValuePersistence */ |
| 38 | private $sessionPersistence; |
| 39 | |
| 40 | protected function getPersistence() |
| 41 | { |
| 42 | if (is_null($this->sessionPersistence)) { |
| 43 | $this->sessionPersistence = common_persistence_KeyValuePersistence::getPersistence( |
| 44 | $this->getOption(self::OPTION_PERSISTENCE) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | return $this->sessionPersistence; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * (non-PHPdoc) |
| 53 | * @see common_session_storage_SessionStorage::open() |
| 54 | */ |
| 55 | public function open($savePath, $sessionName) |
| 56 | { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * (non-PHPdoc) |
| 62 | * @see common_session_storage_SessionStorage::close() |
| 63 | */ |
| 64 | public function close() |
| 65 | { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * (non-PHPdoc) |
| 71 | * @see common_session_storage_SessionStorage::read() |
| 72 | */ |
| 73 | public function read($id) |
| 74 | { |
| 75 | $session = $this->getPersistence()->get(self::KEY_NAMESPACE . $id); |
| 76 | return is_string($session) ? $session : ''; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * (non-PHPdoc) |
| 81 | * @see common_session_storage_SessionStorage::write() |
| 82 | */ |
| 83 | public function write($id, $data) |
| 84 | { |
| 85 | return $this->getPersistence()->set(self::KEY_NAMESPACE . $id, $data, $this->getSessionTtl()); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * (non-PHPdoc) |
| 90 | * @see common_session_storage_SessionStorage::destroy() |
| 91 | */ |
| 92 | public function destroy($id) |
| 93 | { |
| 94 | $this->getPersistence()->del(self::KEY_NAMESPACE . $id); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * (non-PHPdoc) |
| 100 | * @see common_session_storage_SessionStorage::gc() |
| 101 | */ |
| 102 | public function gc($maxlifetime) |
| 103 | { |
| 104 | // |
| 105 | //problem here either |
| 106 | // solution 1 : do two explicit handlers for each specific persistence (Redis, SQL) |
| 107 | // solution 2 : Check if the eprsistence is capable of autonomous garbage |
| 108 | // |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | private function getSessionTtl(): int |
| 113 | { |
| 114 | return $this->getOption(self::OPTION_SESSION_TTL) ?? (int)ini_get('session.gc_maxlifetime'); |
| 115 | } |
| 116 | } |