]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php
More object are configureable, cache initially added
[shipsimu.git] / inc / classes / main / database / wrapper / class_UserDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for the User class
4  *
5  * @see                 DatabaseFrontendInterface - An interface for database frontends (front-end to the application)
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0.0
8  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.ship-simu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class UserDatabaseWrapper extends BaseDatabaseWrapper {
26         /**
27          * Cache instance
28          */
29         private $cacheInstance = null;
30
31         // Constants
32         const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0xe00;
33
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct() {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42
43                 // Set part description
44                 $this->setObjectDescription("Database wrapper for user objects");
45
46                 // Create unique ID number
47                 $this->createUniqueID();
48
49                 // Initialize the cache instance
50                 $this->initCacheInstance();
51         }
52
53         /**
54          * Creates an instance of this database wrapper by a provided user class
55          *
56          * @param       $userInstance           An instance of a user class
57          * @return      $wrapperInstance        An instance of the created wrapper class
58          * @throws      WrapperUserNameNotFoundException        If the supplied username
59          *                                                                                              does not exist
60          */
61         public final static function createUserDatabaseWrapper (ManageableUser $userInstance) {
62                 // Get a new instance
63                 $wrapperInstance = new UserDatabaseWrapper();
64
65                 // Does the username exists?
66                 if (!$wrapperInstance->ifUserNameExists($userInstance->getUserName())) {
67                         // Throw an exception here
68                         throw new WrapperUserNameNotFoundException (array($wrapperInstance, $userInstance), self::EXCEPTION_CLIENT_USERNAME_NOT_FOUND);
69                 }
70
71                 // The user exists
72                 $wrapperInstance->partialStub("Add loading of full user details");
73
74                 // Return the instance
75                 return $wrapperInstance;
76         }
77
78         /**
79          * Initializes the cache instance with a new object
80          *
81          * @return      void
82          */
83         private function initCacheInstance () {
84                 // Set the new instance
85                 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
86         }
87
88         /**
89          * Checks wether the given username is already used
90          *
91          * @param       $userName       The username we shall look up
92          * @return      $exists         Wether the username exists
93          */
94         public function ifUserNameExists ($userName) {
95                 // By default no entry exist
96                 $exists = false;
97
98                 // Does this entry exist?
99                 if ($this->cacheInstance->offsetExists($userName)) {
100                         // Then we have a user!
101                         $exists = true;
102                 } else {
103                         // Create a search criteria
104                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
105
106                         // Add the username as a criteria and set limit to one entry
107                         $criteriaInstance->add(self::DB_USERNAME, $userName);
108                         $criteriaInstance->setLimit(1);
109
110                         // Get a search result
111                         $result = $this->doSelectByCriteria($criteriaInstance);
112
113                         // Search for it
114                         if ($result->next()) {
115                                 // Get the result
116                                 $this->cacheInstance->add($userName, $result->getResultArray());
117
118                                 // Entry found, so all is fine
119                                 $exists = true;
120                         }
121                 }
122
123                 // Return the result
124                 return $exists;
125         }
126 }
127
128 // [EOF]
129 ?>