3 namespace Org\Mxchange\CoreFramework\Database\Frontend\User;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Database\Frontend\BaseDatabaseFrontend;
8 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Frontend\Account\ManageableAccountFrontend;
10 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
11 use Org\Mxchange\CoreFramework\Registration\User\UserRegister;
12 use Org\Mxchange\CoreFramework\Registry\Registerable;
13 use Org\Mxchange\CoreFramework\Result\Update\UpdateableResult;
16 * A database frontend for the User class
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class UserDatabaseFrontend extends BaseDatabaseFrontend implements ManageableAccountFrontend, Registerable {
38 // Constants for exceptions
39 const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180;
41 // Constants for database columns
42 const DB_COLUMN_USERID = 'userid';
43 const DB_COLUMN_USERNAME = 'username';
44 const DB_COLUMN_EMAIL = 'email';
45 const DB_COLUMN_CONFIRM_HASH = 'confirm_hash';
46 const DB_COLUMN_USER_STATUS = 'user_status';
48 // Constants for database table names
49 const DB_TABLE_USER = 'user';
52 * Protected constructor
56 private function __construct () {
57 // Call parent constructor
58 parent::__construct(__CLASS__);
62 * Creates an instance of this database frontend by a provided user class
64 * @return $frontendInstance An instance of the created frontend class
66 public static final function createUserDatabaseFrontend () {
68 $frontendInstance = new UserDatabaseFrontend();
70 // Set (primary!) table name
71 $frontendInstance->setTableName(self::DB_TABLE_USER);
73 // Return the instance
74 return $frontendInstance;
78 * Handles inserting the registration data from a registration instance into the database
80 * @param $registrationInstance An instance of a registration class
83 public function insertRegistrationObject (UserRegister $registrationInstance) {
84 // Generate a data set for the request
85 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
87 // Set the primary key
88 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
90 // Add registration elements to the dataset
91 $registrationInstance->addElementsToDataSet($dataSetInstance);
93 // "Insert" this request instance completely into the database
94 $this->queryInsertDataSet($dataSetInstance);
98 * Updates an user database entry with given result
100 * @param $resultInstance An instance of a UpdateableResult class
102 * @throws NullPointerException If $updateInstance or $searchInstance is null
104 public function doUpdateByResult (UpdateableResult $resultInstance) {
105 // Get the search instance from result
106 $searchInstance = $resultInstance->getSearchInstance();
109 if (is_null($searchInstance)) {
110 // Get the update instance
111 $updateInstance = $resultInstance->getUpdateInstance();
114 if (is_null($updateInstance)) {
115 // Throw an exception here
116 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
119 // Get search instance from update instance
120 $searchInstance = $updateInstance->getSearchInstance();
123 if (is_null($searchInstance)) {
124 // Throw an exception here
125 throw new NullPointerException($updateInstance, self::EXCEPTION_IS_NULL_POINTER);
129 // Generate a data set object
130 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
132 // Add seach criteria
133 $dataSetInstance->setSearchInstance($searchInstance);
135 // Set the primary key
136 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
138 // Add all update criteria to the database set
139 $resultInstance->addElementsToDataSet($dataSetInstance);
141 // "Update" this request with the database
142 FrameworkBootstrap::getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);