Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update namespaces to better reflect adapters #78

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace Utopia\Abuse\Adapters;
namespace Utopia\Abuse\Adapters\Database;

use Utopia\Abuse\Adapters\TimeLimit as TimeLimitAdapter;
use Utopia\Database\Database as UtopiaDB;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
Expand All @@ -12,7 +13,7 @@
use Utopia\Database\Validator\Authorization;
use Utopia\Exception;

class Database extends TimeLimit
class TimeLimit extends TimeLimitAdapter
{
public const COLLECTION = 'abuse';

Expand Down Expand Up @@ -99,7 +100,7 @@ public function setup(): void
];

try {
$this->db->createCollection(Database::COLLECTION, $attributes, $indexes);
$this->db->createCollection(TimeLimit::COLLECTION, $attributes, $indexes);
} catch (Duplicate) {
// Collection already exists
}
Expand Down Expand Up @@ -128,7 +129,7 @@ protected function count(string $key, string $datetime): int

/** @var array<Document> $result */
$result = Authorization::skip(function () use ($key, $datetime) {
return $this->db->find(Database::COLLECTION, [
return $this->db->find(TimeLimit::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
]);
Expand Down Expand Up @@ -160,7 +161,7 @@ protected function hit(string $key, string $datetime): void
}

Authorization::skip(function () use ($datetime, $key) {
$data = $this->db->findOne(Database::COLLECTION, [
$data = $this->db->findOne(TimeLimit::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
]);
Expand All @@ -171,14 +172,14 @@ protected function hit(string $key, string $datetime): void
'key' => $key,
'time' => $datetime,
'count' => 1,
'$collection' => Database::COLLECTION,
'$collection' => TimeLimit::COLLECTION,
];

try {
$this->db->createDocument(Database::COLLECTION, new Document($data));
$this->db->createDocument(TimeLimit::COLLECTION, new Document($data));
} catch (Duplicate $e) {
// Duplicate in case of race condition
$data = $this->db->findOne(Database::COLLECTION, [
$data = $this->db->findOne(TimeLimit::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
]);
Expand All @@ -188,14 +189,14 @@ protected function hit(string $key, string $datetime): void
if (\is_numeric($count)) {
$this->count = intval($count);
}
$this->db->increaseDocumentAttribute(Database::COLLECTION, $data->getId(), 'count');
$this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(), 'count');
} else {
throw new \Exception('Document Not Found');
}
}
} else {
/** @var Document $data */
$this->db->increaseDocumentAttribute(Database::COLLECTION, $data->getId(), 'count');
$this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(), 'count');
}
});

Expand Down Expand Up @@ -227,7 +228,7 @@ public function getLogs(?int $offset = null, ?int $limit = 25): array
$queries[] = Query::limit($limit);
}

return $this->db->find(Database::COLLECTION, $queries);
return $this->db->find(TimeLimit::COLLECTION, $queries);
});

return $results;
Expand All @@ -245,12 +246,12 @@ public function cleanup(string $datetime): bool
{
Authorization::skip(function () use ($datetime) {
do {
$documents = $this->db->find(Database::COLLECTION, [
$documents = $this->db->find(TimeLimit::COLLECTION, [
Query::lessThan('time', $datetime),
]);

foreach ($documents as $document) {
$this->db->deleteDocument(Database::COLLECTION, $document->getId());
$this->db->deleteDocument(TimeLimit::COLLECTION, $document->getId());
}
} while (! empty($documents));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace Utopia\Abuse\Adapters;
namespace Utopia\Abuse\Adapters\Redis;

use Redis as Client;
use Utopia\Abuse\Adapters\TimeLimit as TimeLimitAdapter;

class Redis extends TimeLimit
class TimeLimit extends TimeLimitAdapter
{
public const NAMESPACE = 'abuse';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php

namespace Utopia\Tests\Bench;
namespace Abuse\Bench\Database;

use PDO;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapters\Database as AdaptersDatabase;
use Utopia\Abuse\Adapters\Database\TimeLimit;
use Utopia\Cache\Adapter\None as NoCache;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Database;
use Utopia\Exception;
use Utopia\Tests\Bench\Base;

class DatabaseBench extends Base
class TimeLimitBench extends Base
{
protected Database $db;

Expand All @@ -35,7 +36,7 @@ public function setUp(): void
$db->setNamespace('namespace');
$this->db = $db;

$adapter = new AdaptersDatabase('login-attempt-from-{{ip}}', 3, 60 * 5, $db);
$adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 60 * 5, $db);
if (! $db->exists('utopiaTests')) {
$db->create();
$adapter->setup();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace Utopia\Tests\Bench;
namespace Abuse\Bench\Redis;

use Redis as Client;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapters\Redis;
use Utopia\Abuse\Adapters\Redis\TimeLimit;
use Utopia\Tests\Bench\Base;

final class RedisBench extends Base
final class TimeLimitBench extends Base
{
protected Client $redis;

Expand All @@ -17,7 +18,7 @@ public function setUp(): void
{
$this->redis = new Client();
$this->redis->connect('redis', 6379);
$this->adapter = new Redis('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis);
$this->adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis);
$this->abuse = new Abuse($this->adapter);
}
}
2 changes: 1 addition & 1 deletion tests/Abuse/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PDO;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapter;
use Utopia\Abuse\Adapters\Database as AdaptersDatabase;
use Utopia\Abuse\Adapters\Database\TimeLimit as AdaptersDatabase;
use Utopia\Cache\Adapter\None as NoCache;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MariaDB;
Expand Down
10 changes: 5 additions & 5 deletions tests/Abuse/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Utopia\Tests;

use DateInterval;
use Utopia\Abuse\Adapter;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapters\Redis;
use Redis as Client;
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapter;
use Utopia\Abuse\Adapters\Redis\TimeLimit;
use Utopia\Exception;

class RedisTest extends Base
Expand All @@ -21,14 +21,14 @@ public function setUp(): void
{
$this->redis = new Client();
$this->redis->connect('redis', 6379);
$adapter = new Redis('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis);
$adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis);
$adapter->setParam('{{ip}}', '127.0.0.1');
$this->abuse = new Abuse($adapter);
}

public function getAdapter(string $key, int $limit, int $seconds): Adapter
{
return new Redis($key, $limit, $seconds, $this->redis);
return new TimeLimit($key, $limit, $seconds, $this->redis);
}

public function getCleanupDateTime(): string
Expand Down
Loading