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