aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2024-11-27 19:18:12 +0100
committerHarald Eilertsen <haraldei@anduin.net>2025-06-29 13:25:20 +0200
commit91203660852d3f4376e14fb8733a0a9bdb1bd151 (patch)
tree7c61e6990b9c40561acbaadfc801bb3cfc0cfce1
parent07c2cca55c5ae1f14ae9a4e6058844ed5a0ca406 (diff)
downloadvolse-hubzilla-module-admin-accounts-enhancements-continued.tar.gz
volse-hubzilla-module-admin-accounts-enhancements-continued.tar.bz2
volse-hubzilla-module-admin-accounts-enhancements-continued.zip
Move database query out of Entity\Registration.module-admin-accounts-enhancements-continued
This introduces a wrapper class Zotlabs\Data\Registration, that handled the interaction with the database, and that will _own_ the entity. With this model entities will not be handled directly by the user code, but we still get the separation between the business logic (in the entity) and the database related code. The only thing that bothers me is that we now have classes with the same name in both the Entity and the Data subnamespaces. This is fixable, however, but could cause some confustion right now.
-rw-r--r--Zotlabs/Data/Registration.php66
-rw-r--r--Zotlabs/Entity/Registration.php17
-rw-r--r--include/account.php2
-rw-r--r--tests/unit/Data/RegistrationTest.php (renamed from tests/unit/Entity/RegistrationTest.php)4
4 files changed, 69 insertions, 20 deletions
diff --git a/Zotlabs/Data/Registration.php b/Zotlabs/Data/Registration.php
new file mode 100644
index 000000000..f7b1015b9
--- /dev/null
+++ b/Zotlabs/Data/Registration.php
@@ -0,0 +1,66 @@
+<?php
+/*
+ * SPDX-FileCopyrightText: 2024 Hubzilla Community
+ * SPDX-FileContributor: Harald Eilertsen
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+namespace Zotlabs\Data;
+
+use Zotlabs\Entity;
+
+/**
+ * A data model for registrations.
+ */
+class Registration {
+
+ // We offload the actual content of the registration
+ // to the Registration Entity class.
+ private Entity\Registration $entity;
+
+ /**
+ * Find a registration by it's hash.
+ *
+ * @param string $hash The hash of the registration to find.
+ *
+ * @return Registration|null An initialized Registration object
+ * if the registration was found, null otherwise.
+ */
+ public static function find_by_hash(string $hash): ?self
+ {
+ $reg = q("SELECT * FROM register WHERE reg_hash = '%s' LIMIT 1",
+ dbesc($hash)
+ );
+
+ if ($reg) {
+ return new self($reg[0]);
+ }
+
+ return null;
+ }
+
+ /**
+ * Creates a new Registration object from an array or properties.
+ *
+ * @param array $properties An array of property name, value pairs.
+ * The property name is the key in the array, the value is the value.
+ */
+ private function __construct(array $properties) {
+ $this->entity = new Entity\Registration($properties);
+ }
+
+ /**
+ * Delegate method calls to the Registration entity.
+ */
+ public function __call(string $name, array $args): mixed {
+ return call_user_func([$this->entity, $name], $args);
+ }
+
+ /**
+ * Delegate property references to the Registration entity.
+ */
+ public function __get(string $name): mixed {
+ return $this->entity->{$name};
+ }
+}
diff --git a/Zotlabs/Entity/Registration.php b/Zotlabs/Entity/Registration.php
index d210f4d39..e2af8e22b 100644
--- a/Zotlabs/Entity/Registration.php
+++ b/Zotlabs/Entity/Registration.php
@@ -53,23 +53,6 @@ class Registration
}
/**
- * Find a registration by it's hash.
- *
- * @param string $hash The hash of the registration to find.
- *
- * @return Registration|null An initialized Registration object
- * if the registration was found, null otherwise.
- */
- public static function find_by_hash(string $hash): ?self
- {
- $reg = q("SELECT * FROM register WHERE reg_hash = '%s' LIMIT 1",
- dbesc($hash)
- );
-
- return $reg ? new self($reg[0], true) : null;
- }
-
- /**
* Set the value of a property.
*
* @param string $name The name of the property, optionally prefixed by `reg_`.
diff --git a/include/account.php b/include/account.php
index 19ee42457..fce8dc514 100644
--- a/include/account.php
+++ b/include/account.php
@@ -4,9 +4,9 @@
* @brief Somme account related functions.
*/
-use Zotlabs\Entity\Registration;
use Zotlabs\Lib\Config;
use Zotlabs\Lib\Crypto;
+use Zotlabs\Data\Registration;
require_once('include/config.php');
require_once('include/network.php');
diff --git a/tests/unit/Entity/RegistrationTest.php b/tests/unit/Data/RegistrationTest.php
index b705bd516..4799b95f3 100644
--- a/tests/unit/Entity/RegistrationTest.php
+++ b/tests/unit/Data/RegistrationTest.php
@@ -6,9 +6,9 @@
* SPDX-License-Identifier: MIT
*/
-namespace Zotlabs\Tests\Unit\Entity;
+namespace Zotlabs\Tests\Unit\Data;
-use Zotlabs\Entity\Registration;
+use Zotlabs\Data\Registration;
use Zotlabs\Tests\Unit\UnitTestCase;
class RegistrationTest extends UnitTestCase {