From e404c21e8dcf6a310bc83cf1d74e716b105598fa Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 23 Oct 2024 17:29:12 +1300 Subject: [PATCH] Expose attributes and indexes --- src/Abuse/Adapters/Database/TimeLimit.php | 101 ++++++++++++---------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/src/Abuse/Adapters/Database/TimeLimit.php b/src/Abuse/Adapters/Database/TimeLimit.php index 0d408295ed..b6947539e8 100644 --- a/src/Abuse/Adapters/Database/TimeLimit.php +++ b/src/Abuse/Adapters/Database/TimeLimit.php @@ -17,6 +17,50 @@ class TimeLimit extends TimeLimitAdapter { public const COLLECTION = 'abuse'; + public const ATTRIBUTES = [ + [ + '$id' => 'key', + 'type' => UtopiaDB::VAR_STRING, + 'size' => UtopiaDB::LENGTH_KEY, + 'required' => true, + 'signed' => true, + 'array' => false, + 'filters' => [], + ], [ + '$id' => 'time', + 'type' => UtopiaDB::VAR_DATETIME, + 'size' => 0, + 'required' => true, + 'signed' => false, + 'array' => false, + 'filters' => ['datetime'], + ], [ + '$id' => 'count', + 'type' => UtopiaDB::VAR_INTEGER, + 'size' => 11, + 'required' => true, + 'signed' => false, + 'array' => false, + 'filters' => [], + ], + ]; + + public const INDEXES = [ + [ + '$id' => 'unique1', + 'type' => UtopiaDB::INDEX_UNIQUE, + 'attributes' => ['key', 'time'], + 'lengths' => [], + 'orders' => [], + ], [ + '$id' => 'index2', + 'type' => UtopiaDB::INDEX_KEY, + 'attributes' => ['time'], + 'lengths' => [], + 'orders' => [], + ], + ]; + /** * @var UtopiaDB */ @@ -52,55 +96,20 @@ class TimeLimit extends TimeLimitAdapter throw new Exception('You need to create database before running timelimit setup'); } - $attributes = [ - new Document([ - '$id' => 'key', - 'type' => UtopiaDB::VAR_STRING, - 'size' => UtopiaDB::LENGTH_KEY, - 'required' => true, - 'signed' => true, - 'array' => false, - 'filters' => [], - ]), - new Document([ - '$id' => 'time', - 'type' => UtopiaDB::VAR_DATETIME, - 'size' => 0, - 'required' => true, - 'signed' => false, - 'array' => false, - 'filters' => ['datetime'], - ]), - new Document([ - '$id' => 'count', - 'type' => UtopiaDB::VAR_INTEGER, - 'size' => 11, - 'required' => true, - 'signed' => false, - 'array' => false, - 'filters' => [], - ]), - ]; + $attributes = \array_map(function ($attribute) { + return new Document($attribute); + }, self::ATTRIBUTES); - $indexes = [ - new Document([ - '$id' => 'unique1', - 'type' => UtopiaDB::INDEX_UNIQUE, - 'attributes' => ['key', 'time'], - 'lengths' => [], - 'orders' => [], - ]), - new Document([ - '$id' => 'index2', - 'type' => UtopiaDB::INDEX_KEY, - 'attributes' => ['time'], - 'lengths' => [], - 'orders' => [], - ]), - ]; + $indexes = \array_map(function ($index) { + return new Document($index); + }, self::INDEXES); try { - $this->db->createCollection(TimeLimit::COLLECTION, $attributes, $indexes); + $this->db->createCollection( + TimeLimit::COLLECTION, + $attributes, + $indexes + ); } catch (Duplicate) { // Collection already exists }