Skip to content

Commit

Permalink
Verify DateTimeImmutable#modify() outcomes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Sep 2, 2022
1 parent 485fc43 commit 88d62f0
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Storageless/Http/SessionMiddleware.php
Expand Up @@ -46,6 +46,7 @@
use PSR7Sessions\Storageless\Session\SessionInterface;
use stdClass;

use function assert;
use function date_default_timezone_get;
use function sprintf;

Expand All @@ -55,9 +56,7 @@ final class SessionMiddleware implements MiddlewareInterface
public const SESSION_ATTRIBUTE = 'session';
public const DEFAULT_COOKIE = '__Secure-slsession';
public const DEFAULT_REFRESH_TIME = 60;

private Configuration $config;

private SetCookie $defaultCookie;

public function __construct(
Expand Down Expand Up @@ -209,8 +208,12 @@ private function appendToken(SessionInterface $sessionContainer, Response $respo

private function shouldTokenBeRefreshed(Token|null $token): bool
{
$refreshTime = $this->clock->now()->sub(new DateInterval(sprintf('PT%sS', $this->refreshTime)));

assert($refreshTime !== false);

return $token !== null
&& $token->hasBeenIssuedBefore($this->clock->now()->sub(new DateInterval(sprintf('PT%sS', $this->refreshTime))));
&& $token->hasBeenIssuedBefore($refreshTime);
}

/** @throws BadMethodCallException */
Expand All @@ -237,6 +240,8 @@ private function getExpirationCookie(): SetCookie
{
$expirationDate = $this->clock->now()->modify('-30 days');

assert($expirationDate !== false);

return $this
->defaultCookie
->withValue(null)
Expand Down

0 comments on commit 88d62f0

Please sign in to comment.