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