Lines
97.22%
35 / 36
Methods
87.50%
7 / 8
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| getLastInstance | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| setHttpHandler | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| open | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| close | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| read | 94.44% | 17 / 18 | 0.00% | 0 / 1 | 9.01 | ||
| write | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | ||
| destroy | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| gc | n/a | 0 / 0 | n/a | 0 / 0 | 1 | ||
| 7 | class SessionHandler implements \SessionHandlerInterface | |
| 8 | { | |
| 9 | public ?string $sessionName = null; | |
| 10 | ||
| 11 | /** | |
| 12 | * Adds a parameter "sync" on reading the session from the API | |
| 13 | * Use a value of 1 to enable synchronous reads | |
| 14 | * if a former session write happened during a redirect | |
| 15 | */ | |
| 16 | public static int $useSyncFlag = 0; | |
| 17 | ||
| 18 | protected static ?self $lastInstance = null; | |
| 19 | ||
| 20 | /** | |
| 21 | * @var \BO\Zmsclient\Http $http | |
| 22 | * | |
| 23 | */ | |
| 24 | protected Http $http; | |
| 25 | ||
| 26 | ||
| 27 | public function __construct(Http $http) | |
| 28 | { | |
| 29 | $this->setHttpHandler($http); | |
| 30 | static::$lastInstance = $this; | |
| 31 | } | |
| 32 | ||
| 33 | /** @psalm-api */ | |
| 34 | public static function getLastInstance(): ?self | |
| 35 | { | |
| 36 | return static::$lastInstance; | |
| 37 | } | |
| 38 | ||
| 39 | public function setHttpHandler(Http $http): void | |
| 40 | { | |
| 41 | $this->http = $http; | |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * @SuppressWarnings(UnusedFormalParameter) | |
| 46 | */ | |
| 47 | #[\Override] | |
| 48 | public function open(string $path, string $name): bool | |
| 49 | { | |
| 50 | $this->sessionName = $name; | |
| 51 | return true; | |
| 52 | } | |
| 53 | ||
| 54 | /** @psalm-api Required by SessionHandlerInterface and used by PHP session shutdown. */ | |
| 55 | #[\Override] | |
| 56 | public function close(): bool | |
| 57 | { | |
| 58 | return true; | |
| 59 | } | |
| 60 | ||
| 61 | #[\Override] | |
| 62 | public function read(string $id, array $params = []): string | |
| 63 | { | |
| 64 | $hashedSessionId = hash('sha256', $id); | |
| 65 | $params['sync'] = static::$useSyncFlag; | |
| 66 | try { | |
| 67 | $session = $this->http->readGetResult( | |
| 68 | '/session/' . (string) $this->sessionName . '/' . $hashedSessionId . '/', | |
| 69 | $params | |
| 70 | ) | |
| 71 | ->getEntity(); | |
| 72 | } catch (Exception\ApiFailed $exception) { | |
| 73 | throw $exception; | |
| 74 | } catch (Exception $exception) { | |
| 75 | if ($exception->getCode() == 404) { | |
| 76 | $session = false; | |
| 77 | } else { | |
| 78 | throw $exception; | |
| 79 | } | |
| 80 | } | |
| 81 | if (isset($params['oidc']) && 1 == $params['oidc'] && $session instanceof Session) { | |
| 82 | $session = $session->withOidcDataOnly(); | |
| 83 | } | |
| 84 | return ($session instanceof Session && isset($session['content'])) | |
| 85 | ? serialize($session->getContent()) | |
| 86 | : ''; | |
| 87 | } | |
| 88 | ||
| 89 | #[\Override] | |
| 90 | public function write(string $id, string $data, array $params = []): bool | |
| 91 | { | |
| 92 | $hashedSessionId = hash('sha256', $id); | |
| 93 | $entity = new Session(); | |
| 94 | $entity['id'] = $hashedSessionId; | |
| 95 | $entity['name'] = $this->sessionName; | |
| 96 | $entity['content'] = unserialize($data); | |
| 97 | ||
| 98 | $session = $this->http->readPostResult('/session/', $entity, $params) | |
| 99 | ->getEntity(); | |
| 100 | ||
| 101 | return (null !== $session) ? true : false; | |
| 102 | } | |
| 103 | ||
| 104 | #[\Override] | |
| 105 | public function destroy(string $id): bool | |
| 106 | { | |
| 107 | $hashedSessionId = hash('sha256', $id); | |
| 108 | $this->http->readDeleteResult('/session/' . (string) $this->sessionName . '/' . $hashedSessionId . '/'); | |
| 109 | return true; | |
| 110 | } | |
| 111 | ||
| 112 | /** | |
| 113 | * @SuppressWarnings(UnusedFormalParameter) | |
| 114 | * @SuppressWarnings(ShortMethodName) | |
| 115 | * @codeCoverageIgnore | |
| 116 | * @psalm-api | |
| 117 | */ | |
| 118 | #[\Override] | |
| 119 | public function gc(int $max_lifetime): int|false | |
| 120 | { | |
| 121 | /* | |
| 122 | * $compareTs = time() - $max_lifetime; | |
| 123 | * $query = ' | |
| 124 | * DELETE FROM | |
| 125 | * sessiondata | |
| 126 | * WHERE | |
| 127 | * UNIX_TIMESTAMP(`ts`) < ? AND | |
| 128 | * sessionname=? | |
| 129 | * '; | |
| 130 | * $statement = $this->getWriter()->prepare($query); | |
| 131 | * return $statement->execute(array( | |
| 132 | * $compareTs, | |
| 133 | * $this->sessionName | |
| 134 | * )); | |
| 135 | */ | |
| 136 | return 1; | |
| 137 | } | |
| 138 | } |