3 namespace CoreFramework\Database\Wrapper\User;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Generic\NullPointerException;
8 use CoreFramework\Registry\Registerable;
11 * A database wrapper for the User class
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class UserDatabaseWrapper extends BaseDatabaseWrapper implements ManageableAccountWrapper, Registerable {
33 // Constants for exceptions
34 const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180;
36 // Constants for database columns
37 const DB_COLUMN_USERID = 'userid';
38 const DB_COLUMN_USERNAME = 'username';
39 const DB_COLUMN_EMAIL = 'email';
40 const DB_COLUMN_CONFIRM_HASH = 'confirm_hash';
41 const DB_COLUMN_USER_STATUS = 'user_status';
43 // Constants for database table names
44 const DB_TABLE_USER = 'user';
47 * Protected constructor
51 protected function __construct () {
52 // Call parent constructor
53 parent::__construct(__CLASS__);
57 * Creates an instance of this database wrapper by a provided user class
59 * @return $wrapperInstance An instance of the created wrapper class
61 public static final function createUserDatabaseWrapper () {
63 $wrapperInstance = new UserDatabaseWrapper();
65 // Set (primary!) table name
66 $wrapperInstance->setTableName(self::DB_TABLE_USER);
68 // Return the instance
69 return $wrapperInstance;
73 * Handles inserting the registration data from a registration instance into the database
75 * @param $registrationInstance An instance of a registration class
78 public function insertRegistrationObject (UserRegister $registrationInstance) {
79 // Generate a data set for the request
80 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
82 // Set the primary key
83 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
85 // Add registration elements to the dataset
86 $registrationInstance->addElementsToDataSet($dataSetInstance, NULL);
88 // "Insert" this request instance completely into the database
89 $this->queryInsertDataSet($dataSetInstance);
93 * Updates an user database entry with given result
95 * @param $resultInstance An instance of a Updateable database result
97 * @throws NullPointerException If $updateInstance or $searchInstance is null
99 public function doUpdateByResult (UpdateableResult $resultInstance) {
100 // Get the search instance from result
101 $searchInstance = $resultInstance->getSearchInstance();
104 if (is_null($searchInstance)) {
105 // Get the update instance
106 $updateInstance = $resultInstance->getUpdateInstance();
109 if (is_null($updateInstance)) {
110 // Throw an exception here
111 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
114 // Get search instance from update instance
115 $searchInstance = $updateInstance->getSearchInstance();
118 if (is_null($searchInstance)) {
119 // Throw an exception here
120 throw new NullPointerException($updateInstance, self::EXCEPTION_IS_NULL_POINTER);
124 // Generate a data set object
125 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
127 // Add seach criteria
128 $dataSetInstance->setSearchInstance($searchInstance);
130 // Set the primary key
131 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
133 // Add all update criteria to the database set
134 $resultInstance->addElementsToDataSet($dataSetInstance, NULL);
136 // "Update" this request with the database
137 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);