public function create($userId = 1)
{
AmyLogger::logn('Creating session for:', $userId);
if (!is_numeric($userId)) {
AmyLogger::logn("Invalid user ID specified: `{$userId}'");
throw new Exception("Invalid user ID specified: `{$userId}'");
}
try {
$this->authorize();
if ($userId == $this->authInfo['user_id']) {
AmyLogger::logn("Already authorized, renewing.", $this->authInfo);
if (time() + self::RENEW_BEFORE_EXPIRATION < $this->authInfo['expired_at']) {
AmyLogger::logn("Not necessary to renew yet.");
// not renewing until one hour before expiration to avoid overhead
return $this;
}
// let's renew it
$this->authInfo['expired_at'] = time() + self::EXPIRES_AFTER_SECONDS;
$this->save();
AmyLogger::logn("Renewed successfully");
return $this;
}
} catch (Exception $e) {
}
$this->authInfo['user_id'] = $userId;
$this->authInfo['expired_at'] = time() + self::EXPIRES_AFTER_SECONDS;
$this->authInfo['token'] = $this->generate_hash();
$this->save();
AmyLogger::logn('Setting cookie for newly created session: ', $this->authInfo);
setcookie('amy_token', $this->authInfo['token'], 0, '/');
return $this;
}