Skip to content

Commit b52cfa6

Browse files
committed
update README + add deleteAnonymousSessions
1 parent 0d95594 commit b52cfa6

File tree

3 files changed

+126
-55
lines changed

3 files changed

+126
-55
lines changed

README.md

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ composer require rancoud/session
1818
By default session is in read only (option read_and_close = 1).
1919
You can specify it using `Session::setReadWrite()` or `Session::setReadOnly()`
2020

21-
Session::start() is not needed, but:
21+
Session::start() is not needed, but:
2222
* Session will automatically start in read only when using `get, has, hasKeyAndValue, getAll`
2323
* Session will automatically start in write mode when using `set, remove, getAndRemove, keepFlash, gc, regenerate`
2424

@@ -98,86 +98,88 @@ $value = Session::get('key');
9898
```
9999

100100
## Session
101-
### Static General Commands
102-
* start([options: array = []]): void
103-
* regenerate(): bool
104-
* destroy(): bool
105-
* commit(): void
106-
* rollback(): bool
107-
* unsaved(): bool
108-
* hasStarted(): bool
109-
* getId(): string
110-
* setId(id: string): string
111-
* gc(): void
112-
* setReadOnly(): void
113-
* setReadWrite(): void
101+
### Static General Commands
102+
* start([options: array = []]): void
103+
* regenerate(): bool
104+
* destroy(): bool
105+
* commit(): void
106+
* rollback(): bool
107+
* unsaved(): bool
108+
* hasStarted(): bool
109+
* getId(): string
110+
* setId(id: string): string
111+
* gc(): void
112+
* setReadOnly(): void
113+
* setReadWrite(): void
114114
* isReadOnly(): bool
115115

116116
### Static Variables $_SESSION access
117-
* set(key: string, value: mixed): void
118-
* get(key: string): mixed
119-
* getAll(): array
120-
* getAndRemove(key: string): mixed
121-
* has(key: string): bool
122-
* hasKeyAndValue(key: string, value: mixed): bool
123-
* remove(key: string): void
117+
* set(key: string, value: mixed): void
118+
* get(key: string): mixed
119+
* getAll(): array
120+
* getAndRemove(key: string): mixed
121+
* has(key: string): bool
122+
* hasKeyAndValue(key: string, value: mixed): bool
123+
* remove(key: string): void
124124

125125
### Static Variables flash access
126126
Flash data are store in a separate variable.
127127
They will dissapear at the end of the script execution or after `commit()` `unsaved()`.
128128
You can use keepFlash for saving it in $_SESSION.
129-
When flash data is restore, it will be delete in $_SESSION.
129+
When flash data is restore, it will be delete in $_SESSION.
130130

131-
* setFlash(key: string, value: mixed): void
132-
* getFlash(key: string): mixed
133-
* getAllFlash(): array
134-
* hasFlash(key: string): bool
135-
* hasFlashKeyAndValue(key: string, value: mixed): bool
136-
* keepFlash([keys: array = []]): void
131+
* setFlash(key: string, value: mixed): void
132+
* getFlash(key: string): mixed
133+
* getAllFlash(): array
134+
* hasFlash(key: string): bool
135+
* hasFlashKeyAndValue(key: string, value: mixed): bool
136+
* keepFlash([keys: array = []]): void
137137

138-
### Static Options
139-
* setOption(key: string, value): void
140-
* setOptions(options: array): void
141-
* getOption(key: string): mixed
138+
### Static Options
139+
* setOption(key: string, value): void
140+
* setOptions(options: array): void
141+
* getOption(key: string): mixed
142142

143143
### Static Driver
144-
* getDriver(): \SessionHandlerInterface
144+
* getDriver(): \SessionHandlerInterface
145145

146146
#### Static PHP Session Default Driver
147-
* useDefaultDriver(): void
148-
* useDefaultEncryptionDriver(key: string, [method: string|null = null]): void
149-
* setLengthSessionID(length: int): void
150-
* getLengthSessionID(): int
147+
* useDefaultDriver(): void
148+
* useDefaultEncryptionDriver(key: string, [method: string|null = null]): void
149+
* setLengthSessionID(length: int): void
150+
* getLengthSessionID(): int
151151

152152
#### Static File Driver
153-
* useFileDriver(): void
154-
* useFileEncryptionDriver(key: string, [method: string|null = null]): void
155-
* setPrefixForFile(prefix: string): void
153+
* useFileDriver(): void
154+
* useFileEncryptionDriver(key: string, [method: string|null = null]): void
155+
* setPrefixForFile(prefix: string): void
156156
* setLengthSessionID(length: int): void
157157
* getLengthSessionID(): int
158158

159159
#### Static Database Driver
160-
* useNewDatabaseDriver(configuration: \Rancoud\Database\Configurator|array): void
161-
* useCurrentDatabaseDriver(databaseInstance: \Rancoud\Database\Database): void
162-
* useNewDatabaseEncryptionDriver(configuration: \Rancoud\Database\Configurator|array, key: string, [method: string = null]): void
163-
* useCurrentDatabaseEncryptionDriver(databaseInstance: \Rancoud\Database\Database, key: string, [method: string = null]): void
164-
* setUserIdForDatabase(userId: int): void
160+
* useNewDatabaseDriver(configuration: \Rancoud\Database\Configurator|array): void
161+
* useCurrentDatabaseDriver(databaseInstance: \Rancoud\Database\Database): void
162+
* useNewDatabaseEncryptionDriver(configuration: \Rancoud\Database\Configurator|array, key: string, [method: string = null]): void
163+
* useCurrentDatabaseEncryptionDriver(databaseInstance: \Rancoud\Database\Database, key: string, [method: string = null]): void
164+
* setUserIdForDatabase(userId: int): void
165165
* setLengthSessionID(length: int): void
166166
* getLengthSessionID(): int
167+
* deleteUserSessions(int $userID): void
168+
* deleteAnonymousSessions(): int
167169

168170
#### Static Redis Driver
169-
* useNewRedisDriver(configuration: array|string): void
170-
* useCurrentRedisDriver(redisInstance: \Predis\Client): void
171-
* useNewRedisEncryptionDriver(configuration: array|string, key: string, [method: string = null]): void
172-
* useCurrentRedisEncryptionDriver(redisInstance: \Predis\Client, key: string, [method: string = null]): void
171+
* useNewRedisDriver(configuration: array|string): void
172+
* useCurrentRedisDriver(redisInstance: \Predis\Client): void
173+
* useNewRedisEncryptionDriver(configuration: array|string, key: string, [method: string = null]): void
174+
* useCurrentRedisEncryptionDriver(redisInstance: \Predis\Client, key: string, [method: string = null]): void
173175
* setLengthSessionID(length: int): void
174-
* getLengthSessionID(): int
176+
* getLengthSessionID(): int
175177

176178
#### Static Custom Driver
177-
* useCustomDriver(customDriver: \SessionHandlerInterface): void
179+
* useCustomDriver(customDriver: \SessionHandlerInterface): void
178180

179181
## Session options
180-
List of session options you can change:
182+
List of session options you can change:
181183
* save_path
182184
* name
183185
* save_handler

src/Database.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,23 @@ public function getLengthSessionID(): int
5858
}
5959

6060
/** @throws SessionException */
61-
public function deleteUserSessions(int $userID): bool
61+
public function deleteUserSessions(int $userID): void
6262
{
6363
try {
6464
$sql = 'DELETE FROM sessions WHERE id_user = :id_user';
6565
$params = ['id_user' => $userID];
6666
$this->db->delete($sql, $params);
67+
} catch (DatabaseException $e) {
68+
throw new SessionException('could not delete sessions: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
69+
}
70+
}
6771

68-
return true;
72+
/** @throws SessionException */
73+
public function deleteAnonymousSessions(): void
74+
{
75+
try {
76+
$sql = 'DELETE FROM sessions WHERE id_user IS NULL';
77+
$this->db->delete($sql);
6978
} catch (DatabaseException $e) {
7079
throw new SessionException('could not delete sessions: ' . $e->getMessage(), $e->getCode(), $e->getPrevious());
7180
}

tests/DatabaseTest.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,4 +511,64 @@ public function testDeleteUserSessionsSessionException(): void
511511

512512
$database->deleteUserSessions(1);
513513
}
514-
}
514+
515+
/**
516+
* @throws DatabaseException
517+
* @throws SessionException
518+
*/
519+
public function testDeleteAnonymousSessions(): void
520+
{
521+
$database = new Database();
522+
$database->setCurrentDatabase(static::$db);
523+
524+
$data = 'azerty';
525+
526+
$database->setUserId(10);
527+
528+
static::assertTrue($database->write('session_id_10', $data));
529+
static::assertTrue($database->write('session_id_20', $data));
530+
static::assertTrue($database->write('session_id_30', $data));
531+
532+
$database->setUserId(null);
533+
534+
static::assertTrue($database->write('session_id_40', $data));
535+
static::assertTrue($database->write('session_id_50', $data));
536+
static::assertTrue($database->write('session_id_60', $data));
537+
538+
$sql = 'SELECT id FROM sessions WHERE id_user = :id_user';
539+
$params = ['id_user' => 10];
540+
$sessionsFromDatabase = static::$db->selectCol($sql, $params);
541+
static::assertSame(['session_id_10', 'session_id_20', 'session_id_30'], $sessionsFromDatabase);
542+
543+
$sql = 'SELECT id FROM sessions WHERE id_user IS NULL';
544+
$sessionsFromDatabase = static::$db->selectCol($sql);
545+
static::assertSame(['session_id_40', 'session_id_50', 'session_id_60'], $sessionsFromDatabase);
546+
547+
$database->deleteAnonymousSessions(10);
548+
549+
$sql = 'SELECT id FROM sessions';
550+
$sessionsFromDatabase = static::$db->selectCol($sql);
551+
static::assertSame(['session_id_10', 'session_id_20', 'session_id_30'], $sessionsFromDatabase);
552+
}
553+
554+
/**
555+
* @throws DatabaseException
556+
* @throws SessionException
557+
*/
558+
public function testDeleteAnonymousSessionsSessionException(): void
559+
{
560+
$this->expectException(SessionException::class);
561+
$this->expectExceptionMessage('could not delete sessions: Error Connecting Database');
562+
563+
$database = new Database();
564+
$database->setNewDatabase([
565+
'driver' => 'mysql',
566+
'host' => 'mariadb',
567+
'user' => 'invalid',
568+
'password' => '',
569+
'database' => 'test_database'
570+
]);
571+
572+
$database->deleteAnonymousSessions();
573+
}
574+
}

0 commit comments

Comments
 (0)