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