Skip to content

Commit

Permalink
Replaced usage of SessionStorage with an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianwennink committed Oct 28, 2023
1 parent 53710f8 commit 6aee68c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
14 changes: 10 additions & 4 deletions src/Attempts/Drivers/SessionAttempts.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace IconCaptcha\Attempts\Drivers;

use IconCaptcha\Attempts\Attempts;
use IconCaptcha\Storage\Session\SessionStorage;
use IconCaptcha\Storage\Session\SessionStorageInterface;
use IconCaptcha\Utils;

class SessionAttempts extends Attempts
Expand All @@ -21,11 +21,17 @@ class SessionAttempts extends Attempts
private string $sessionKey = 'attempts';

/**
* @var SessionStorage The session storage wrapper.
* @var SessionStorageInterface The session storage wrapper.
*/
private SessionStorage $storage;
private SessionStorageInterface $storage;

public function __construct(SessionStorage $storage, array $options)
/**
* Initializes a new instance of the attempts/timeout manager with session storage.
*
* @param SessionStorageInterface $storage The session storage container.
* @param array $options The captcha storage options.
*/
public function __construct(SessionStorageInterface $storage, array $options)
{
parent::__construct($options);

Expand Down
10 changes: 5 additions & 5 deletions src/Session/Drivers/ServerSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace IconCaptcha\Session\Drivers;

use IconCaptcha\Session\Session;
use IconCaptcha\Storage\Session\SessionStorage;
use IconCaptcha\Storage\Session\SessionStorageInterface;
use IconCaptcha\Utils;

class ServerSession extends Session
Expand All @@ -21,20 +21,20 @@ class ServerSession extends Session
private string $sessionKey = 'challenges';

/**
* @var SessionStorage The session storage container.
* @var SessionStorageInterface The session storage container.
*/
private SessionStorage $storage;
private SessionStorageInterface $storage;

/**
* Initializes a new server session instance.
*
* @param SessionStorage $storage The session storage container.
* @param SessionStorageInterface $storage The session storage container.
* @param array $options The captcha session options.
* @param string $ipAddress The IP address of the visitor.
* @param string $widgetId The captcha widget identifier.
* @param string|null $challengeId The captcha challenge identifier.
*/
public function __construct(SessionStorage $storage, array $options, string $ipAddress, string $widgetId, string $challengeId = null)
public function __construct(SessionStorageInterface $storage, array $options, string $ipAddress, string $widgetId, string $challengeId = null)
{
parent::__construct($options, $ipAddress, $widgetId, $challengeId);

Expand Down
19 changes: 5 additions & 14 deletions src/Storage/Session/SessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace IconCaptcha\Storage\Session;

class SessionStorage
class SessionStorage implements SessionStorageInterface
{
/**
* @var string The session name.
Expand All @@ -27,9 +27,7 @@ public function __construct(string $sessionName)
}

/**
* Reads a value from the session storage.
* @param string $key The key of the value to read. Dot notation is supported.
* @return mixed|null The value if it exists, or null if it does not exist.
* @inheritDoc
*/
public function read(string $key)
{
Expand All @@ -47,10 +45,7 @@ public function read(string $key)
}

/**
* Writes a value to the session storage.
*
* @param string $key The key to write the value to. Dot notation is supported.
* @param mixed $value The value to write to the session.
* @inheritDoc
*/
public function write(string $key, $value): void
{
Expand All @@ -60,9 +55,7 @@ public function write(string $key, $value): void
}

/**
* Removes a value from the session storage.
*
* @param string $key The key of the value to remove. Dot notation is supported.
* @inheritDoc
*/
public function remove(string $key): void
{
Expand All @@ -73,9 +66,7 @@ public function remove(string $key): void
}

/**
* Checks if a value exists in the session storage.
*
* @param string $key The key to check for. Dot notation is supported.
* @inheritDoc
*/
public function exists(string $key): bool
{
Expand Down
43 changes: 43 additions & 0 deletions src/Storage/Session/SessionStorageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* IconCaptcha - Copyright 2023, Fabian Wennink (https://www.fabianwennink.nl)
* Licensed under the MIT license: https://www.fabianwennink.nl/projects/IconCaptcha/license
*
* The above copyright notice and license shall be included in all copies or substantial portions of the software.
*/

namespace IconCaptcha\Storage\Session;

interface SessionStorageInterface
{
/**
* Reads a value from the session storage.
*
* @param string $key The key of the value to read. Dot notation is supported.
* @return mixed|null The value if it exists, or null if it does not exist.
*/
public function read(string $key);

/**
* Writes a value to the session storage.
*
* @param string $key The key to write the value to. Dot notation is supported.
* @param mixed $value The value to write to the session.
*/
public function write(string $key, $value): void;

/**
* Removes a value from the session storage.
*
* @param string $key The key of the value to remove. Dot notation is supported.
*/
public function remove(string $key): void;

/**
* Checks if a value exists in the session storage.
*
* @param string $key The key to check for. Dot notation is supported.
*/
public function exists(string $key): bool;
}

0 comments on commit 6aee68c

Please sign in to comment.