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