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