Skip to content

Commit

Permalink
Add more precise types in the DynamoDbSession integration (#1492)
Browse files Browse the repository at this point in the history
  • Loading branch information
stof authored Jul 2, 2023
1 parent ea428d6 commit bc92cce
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Integration/Aws/DynamoDbSession/src/SessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ class SessionHandler implements \SessionHandlerInterface
private $client;

/**
* @var array
* @var array{
* consistent_read?: bool,
* data_attribute: string,
* hash_key: string,
* session_lifetime?: int,
* session_lifetime_attribute: string,
* table_name: string,
* id_separator: string
* }
*/
private $options;

Expand Down Expand Up @@ -54,11 +62,11 @@ class SessionHandler implements \SessionHandlerInterface
public function __construct(DynamoDbClient $client, array $options)
{
$this->client = $client;
$options['data_attribute'] = $options['data_attribute'] ?? 'data';
$options['hash_key'] = $options['hash_key'] ?? 'id';
$options['session_lifetime_attribute'] = $options['session_lifetime_attribute'] ?? 'expires';
$options['id_separator'] = $options['id_separator'] ?? '_';
$this->options = $options;
$this->options['data_attribute'] = $this->options['data_attribute'] ?? 'data';
$this->options['hash_key'] = $this->options['hash_key'] ?? 'id';
$this->options['session_lifetime_attribute'] = $this->options['session_lifetime_attribute'] ?? 'expires';
$this->options['id_separator'] = $this->options['id_separator'] ?? '_';
}

public function setUp(): void
Expand Down Expand Up @@ -104,7 +112,7 @@ public function close()
$id = session_id();

// Make sure the expiration time is updated, even if the write did not occur
if ($this->sessionId !== $id || !$this->sessionWritten) {
if (false !== $id && ($this->sessionId !== $id || !$this->sessionWritten)) {
$this->sessionWritten = $this->doWrite($id, false);
}

Expand Down Expand Up @@ -223,6 +231,9 @@ private function formatId(string $id): string
return trim($this->sessionName . $this->options['id_separator'] . $id, $this->options['id_separator']);
}

/**
* @return array<string, array{S: string}>
*/
private function formatKey(string $key): array
{
return [$this->options['hash_key'] => ['S' => $key]];
Expand Down

0 comments on commit bc92cce

Please sign in to comment.