Skip to content

Commit

Permalink
refactor and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lohanidamodar committed Mar 13, 2024
1 parent 9bfe913 commit fdb39d5
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 241 deletions.
60 changes: 56 additions & 4 deletions src/Abuse/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,68 @@

namespace Utopia\Abuse;

interface Adapter
abstract class Adapter
{
/**
* @var array<string, string>
*/
protected array $params = [];

/**
* @var string
*/
protected string $key = '';

/**
* Check
*
* Checks if number of counts is bigger or smaller than current limit
*
* @return bool
*/
public function check(): bool;
abstract public function check(): bool;

/**
* Set Param
*
* Set custom param for key pattern parsing
*
* @param string $key
* @param string $value
* @return $this
*/
public function setParam(string $key, string $value): self
{
$this->params[$key] = $value;

return $this;
}

/**
* Get Params
*
* Return array of all key params
*
* @return array<string, string>
*/
protected function getParams(): array
{
return $this->params;
}

/**
* Parse key with all custom attached params
*
* @return string
*/
protected function parseKey(): string
{
foreach ($this->getParams() as $key => $value) {
$this->key = \str_replace($key, $value, $this->key);
}

return $this->key;
}

/**
* Get abuse logs
Expand All @@ -22,13 +74,13 @@ public function check(): bool;
* @param int|null $limit
* @return array<string, mixed>
*/
public function getLogs(?int $offset = null, ?int $limit = 25): array;
abstract public function getLogs(?int $offset = null, ?int $limit = 25): array;

/**
* Delete all logs older than $datetime
*
* @param string $datetime
* @return bool
*/
public function cleanup(string $datetime): bool;
abstract public function cleanup(string $datetime): bool;
}
2 changes: 1 addition & 1 deletion src/Abuse/Adapters/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Exception;
use Utopia\Abuse\Adapter;

class ReCaptcha implements Adapter
class ReCaptcha extends Adapter
{
/**
* Use this for communication between your site and Google.
Expand Down
55 changes: 1 addition & 54 deletions src/Abuse/Adapters/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Utopia\Abuse\Adapter;
use Redis as Client;

class Redis implements Adapter
class Redis extends Adapter
{
public const NAMESPACE = 'abuse';

Expand All @@ -14,11 +14,6 @@ class Redis implements Adapter
*/
protected Client $redis;

/**
* @var string
*/
protected string $key = '';

/**
* @var int
*/
Expand All @@ -34,12 +29,6 @@ class Redis implements Adapter
*/
protected ?int $count = null;

/**
* @var array<string, string>
*/
protected array $params = [];


public function __construct(string $key, int $limit, int $seconds, Client $redis)
{
$this->redis = $redis;
Expand All @@ -49,48 +38,6 @@ public function __construct(string $key, int $limit, int $seconds, Client $redis
$this->limit = $limit;
}

/**
* Set Param
*
* Set custom param for key pattern parsing
*
* @param string $key
* @param string $value
* @return $this
*/
public function setParam(string $key, string $value): self
{
$this->params[$key] = $value;

return $this;
}

/**
* Get Params
*
* Return array of all key params
*
* @return array<string, string>
*/
protected function getParams(): array
{
return $this->params;
}

/**
* Parse key with all custom attached params
*
* @return string
*/
protected function parseKey(): string
{
foreach ($this->getParams() as $key => $value) {
$this->key = \str_replace($key, $value, $this->key);
}

return $this->key;
}

/**
* Undocumented function
*
Expand Down
54 changes: 1 addition & 53 deletions src/Abuse/Adapters/TimeLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Utopia\Database\Validator\Authorization;
use Utopia\Exception;

class TimeLimit implements Adapter
class TimeLimit extends Adapter
{
public const COLLECTION = 'abuse';

Expand All @@ -23,11 +23,6 @@ class TimeLimit implements Adapter
*/
protected Database $db;

/**
* @var string
*/
protected string $key = '';

/**
* @var string
*/
Expand All @@ -43,11 +38,6 @@ class TimeLimit implements Adapter
*/
protected ?int $count = null;

/**
* @var array<string, string>
*/
protected array $params = [];

/**
* @param string $key
* @param int $seconds
Expand Down Expand Up @@ -127,48 +117,6 @@ public function setup(): void
}
}

/**
* Set Param
*
* Set custom param for key pattern parsing
*
* @param string $key
* @param string $value
* @return $this
*/
public function setParam(string $key, string $value): self
{
$this->params[$key] = $value;

return $this;
}

/**
* Get Params
*
* Return array of all key params
*
* @return array<string, string>
*/
protected function getParams(): array
{
return $this->params;
}

/**
* Parse key with all custom attached params
*
* @return string
*/
protected function parseKey(): string
{
foreach ($this->getParams() as $key => $value) {
$this->key = \str_replace($key, $value, $this->key);
}

return $this->key;
}

/**
* Check
*
Expand Down
104 changes: 0 additions & 104 deletions tests/Abuse/AbuseTest.php

This file was deleted.

Loading

0 comments on commit fdb39d5

Please sign in to comment.