]> git.mxchange.org Git - core.git/blob - inc/main/classes/database/frontend/class_UserDatabaseWrapper.php
Continued:
[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\Generic\NullPointerException;
8 use CoreFramework\Registry\Registerable;
9
10 /**
11  * A database wrapper for the User class
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
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
18  *
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.
23  *
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.
28  *
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/>.
31  */
32 class UserDatabaseWrapper extends BaseDatabaseWrapper implements ManageableAccountWrapper, Registerable {
33         // Constants for exceptions
34         const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180;
35
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';
42
43         // Constants for database table names
44         const DB_TABLE_USER = 'user';
45
46         /**
47          * Protected constructor
48          *
49          * @return      void
50          */
51         protected function __construct () {
52                 // Call parent constructor
53                 parent::__construct(__CLASS__);
54         }
55
56         /**
57          * Creates an instance of this database wrapper by a provided user class
58          *
59          * @return      $wrapperInstance        An instance of the created wrapper class
60          */
61         public static final function createUserDatabaseWrapper () {
62                 // Get a new instance
63                 $wrapperInstance = new UserDatabaseWrapper();
64
65                 // Set (primary!) table name
66                 $wrapperInstance->setTableName(self::DB_TABLE_USER);
67
68                 // Return the instance
69                 return $wrapperInstance;
70         }
71
72         /**
73          * Handles inserting the registration data from a registration instance into the database
74          *
75          * @param       $registrationInstance   An instance of a registration class
76          * @return      void
77          */
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));
81
82                 // Set the primary key
83                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
84
85                 // Add registration elements to the dataset
86                 $registrationInstance->addElementsToDataSet($dataSetInstance, NULL);
87
88                 // "Insert" this request instance completely into the database
89                 $this->queryInsertDataSet($dataSetInstance);
90         }
91
92         /**
93          * Updates an user database entry with given result
94          *
95          * @param       $resultInstance         An instance of a Updateable database result
96          * @return      void
97          * @throws      NullPointerException    If $updateInstance or $searchInstance is null
98          */
99         public function doUpdateByResult (UpdateableResult $resultInstance) {
100                 // Get the search instance from result
101                 $searchInstance = $resultInstance->getSearchInstance();
102
103                 // Is this null?
104                 if (is_null($searchInstance)) {
105                         // Get the update instance
106                         $updateInstance = $resultInstance->getUpdateInstance();
107
108                         // Is this null?
109                         if (is_null($updateInstance)) {
110                                 // Throw an exception here
111                                 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
112                         } // END - if
113
114                         // Get search instance from update instance
115                         $searchInstance = $updateInstance->getSearchInstance();
116
117                         // Is it still null?
118                         if (is_null($searchInstance)) {
119                                 // Throw an exception here
120                                 throw new NullPointerException($updateInstance, self::EXCEPTION_IS_NULL_POINTER);
121                         } // END - if
122                 } // END - if
123
124                 // Generate a data set object
125                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_USER));
126
127                 // Add seach criteria
128                 $dataSetInstance->setSearchInstance($searchInstance);
129
130                 // Set the primary key
131                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_USERNAME);
132
133                 // Add all update criteria to the database set
134                 $resultInstance->addElementsToDataSet($dataSetInstance, NULL);
135
136                 // "Update" this request with the database
137                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
138         }
139
140 }