3 namespace CoreFramework\Database\Wrapper\User;
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
9 * A database wrapper for the User class
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class UserDatabaseWrapper extends BaseDatabaseWrapper implements ManageableAccountWrapper, Registerable {
31 // Constants for exceptions
32 const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180;
34 // Constants for database columns
35 const DB_COLUMN_USERID = 'userid';
36 const DB_COLUMN_USERNAME = 'username';
37 const DB_COLUMN_EMAIL = 'email';
38 const DB_COLUMN_CONFIRM_HASH = 'confirm_hash';
39 const DB_COLUMN_USER_STATUS = 'user_status';
41 // Constants for database table names
42 const DB_TABLE_USER = 'user';
45 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
55 * Creates an instance of this database wrapper by a provided user class
57 * @return $wrapperInstance An instance of the created wrapper class
59 public static final function createUserDatabaseWrapper () {
61 $wrapperInstance = new UserDatabaseWrapper();
63 // Set (primary!) table name
64 $wrapperInstance->setTableName(self::DB_TABLE_USER);
66 // Return the instance
67 return $wrapperInstance;
71 * Handles inserting the registration data from a registration instance into the database
73 * @param $registrationInstance An instance of a registration class
76 public function insertRegistrationObject (UserRegister $registrationInstance) {
77 // Generate a data set for the request
78 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
80 // Set the primary key
81 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
83 // Add registration elements to the dataset
84 $registrationInstance->addElementsToDataSet($dataSetInstance, NULL);
86 // "Insert" this request instance completely into the database
87 $this->queryInsertDataSet($dataSetInstance);
91 * Updates an user database entry with given result
93 * @param $resultInstance An instance of a Updateable database result
95 * @throws NullPointerException If $updateInstance or $searchInstance is null
97 public function doUpdateByResult (UpdateableResult $resultInstance) {
98 // Get the search instance from result
99 $searchInstance = $resultInstance->getSearchInstance();
102 if (is_null($searchInstance)) {
103 // Get the update instance
104 $updateInstance = $resultInstance->getUpdateInstance();
107 if (is_null($updateInstance)) {
108 // Throw an exception here
109 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
112 // Get search instance from update instance
113 $searchInstance = $updateInstance->getSearchInstance();
116 if (is_null($searchInstance)) {
117 // Throw an exception here
118 throw new NullPointerException($updateInstance, self::EXCEPTION_IS_NULL_POINTER);
122 // Generate a data set object
123 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
125 // Add seach criteria
126 $dataSetInstance->setSearchInstance($searchInstance);
128 // Set the primary key
129 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
131 // Add all update criteria to the database set
132 $resultInstance->addElementsToDataSet($dataSetInstance, NULL);
134 // "Update" this request with the database
135 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);