]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php
More conventions than code added:
[shipsimu.git] / inc / classes / main / database / wrapper / class_UserDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for the User class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class UserDatabaseWrapper extends BaseDatabaseWrapper {
25         /**
26          * Cache instance
27          */
28         private $cacheInstance = null;
29
30         // Constants for exceptions
31         const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x180;
32
33         // Constants for database columns
34         const DB_COLUMN_USERNAME = "username";
35         const DB_COLUMN_EMAIL    = "email";
36
37         // Constants for database table names
38         const DB_TABLE_USER = "user";
39
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         protected function __construct() {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48
49                 // Set part description
50                 $this->setObjectDescription("Database wrapper for user objects");
51
52                 // Create unique ID number
53                 $this->generateUniqueId();
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          * @throws      WrapperUserNameNotFoundException        If the supplied username
61          *                                                                                              does not exist
62          */
63         public final static function createUserDatabaseWrapper () {
64                 // Get a new instance
65                 $wrapperInstance = new UserDatabaseWrapper();
66
67                 // Initialize the cache instance
68                 $wrapperInstance->initCacheInstance();
69
70                 // Return the instance
71                 return $wrapperInstance;
72         }
73
74         /**
75          * Initializes the cache instance with a new object
76          *
77          * @return      void
78          */
79         protected function initCacheInstance () {
80                 // Set the new instance
81                 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
82         }
83
84         /**
85          * Do a "select" query on the user table with the given search criteria and
86          * store it in cache for later usage
87          *
88          * @param       $criteriaInstance       An instance of a Criteria class
89          * @return      $resultInstance         An instance of a database result class
90          */
91         public function doSelectByCriteria (Criteria $criteriaInstance) {
92                 // First get a key suitable for our cache and extend it with this class name
93                 $cacheKey = sprintf("%s@%s",
94                         $this->__toString(),
95                         $criteriaInstance->getCacheKey()
96                 );
97
98                 // Does this key exists in cache?
99                 if ($this->cacheInstance->offsetExists($cacheKey)) {
100                         // Then use this result
101                         $result = $cacheInstance->offsetGet($cacheKey);
102                 } else {
103                         // Now it's time to ask the database layer for this select statement
104                         $result = $this->getDatabaseInstance()->doSelectByTableCriteria(self::DB_TABLE_USER, $criteriaInstance);
105
106                         // Cache the result if not null
107                         if (!is_null($result)) {
108                                 // A valid result has returned from the database layer
109                                 $this->cacheInstance->offsetSet($cacheKey, $result);
110                         } else {
111                                 // This invalid result must be wrapped
112                                 $result = array(
113                                         'status'                => "invalid",
114                                         'exception'             => $this->getDatabaseInstance()->getLastException()
115                                 );
116                         }
117                 }
118
119                 // Create an instance of a DatabaseResult class with the given result
120                 $resultInstance = DatabaseResult::createDatabaseResult($result);
121
122                 // And return the instance
123                 return $resultInstance;
124         }
125
126         /**
127          * Handles inserting the registration data from a registration instance into the database
128          *
129          * @param       $registrationInstance   An instance of a registration class
130          * @return      void
131          */
132         public function insertRegistrationObject (UserRegister $registrationInstance) {
133                 // Generate a data set for the request
134                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class');
135                 $dataSetInstance->setTableName(self::DB_TABLE_USER);
136
137                 // Add registration elements to the dataset
138                 $registrationInstance->addElementsToDataSet($dataSetInstance);
139
140                 // "Insert" this request instance completely into the database
141                 $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
142         }
143
144         /**
145          * Updates an user database entry with given result
146          *
147          * @param       $resultInstance         An instance of a Updateable database result
148          * @return      void
149          */
150         public function doUpdateByResult (UpdateableResult $resultInstance) {
151                 // Generate a data set object
152                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class');
153                 $dataSetInstance->setTableName(self::DB_TABLE_USER);
154
155                 // Add all update criteria to the database set
156                 $resultInstance->addElementsToDataSet($dataSetInstance);
157
158                 // Add seach criteria
159                 $dataSetInstance->setSearchInstance($resultInstance->getSearchInstance());
160
161                 // Add the primary key
162                 $dataSetInstance->setUniqueKey('username');
163
164                 // "Update" this request with the database
165                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
166         }
167 }
168
169 // [EOF]
170 ?>