3 * A database wrapper for the User class
5 * @see DatabaseFrontendInterface - An interface for database frontends (front-end to the application)
6 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
25 class UserDatabaseWrapper extends BaseDatabaseWrapper {
29 private $cacheInstance = null;
32 const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0xe00;
35 * Protected constructor
39 protected function __construct() {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
43 // Set part description
44 $this->setObjectDescription("Database wrapper for user objects");
46 // Create unique ID number
47 $this->createUniqueID();
49 // Initialize the cache instance
50 $this->initCacheInstance();
54 * Creates an instance of this database wrapper by a provided user class
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
61 public final static function createUserDatabaseWrapper (ManageableUser $userInstance) {
63 $wrapperInstance = new UserDatabaseWrapper();
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);
72 $wrapperInstance->partialStub("Add loading of full user details");
74 // Return the instance
75 return $wrapperInstance;
79 * Initializes the cache instance with a new object
83 private function initCacheInstance () {
84 // Set the new instance
85 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
89 * Checks wether the given username is already used
91 * @param $userName The username we shall look up
92 * @return $exists Wether the username exists
94 public function ifUserNameExists ($userName) {
95 // By default no entry exist
98 // Does this entry exist?
99 if ($this->cacheInstance->offsetExists($userName)) {
100 // Then we have a user!
103 // Create a search criteria
104 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
106 // Add the username as a criteria and set limit to one entry
107 $criteriaInstance->add(self::DB_USERNAME, $userName);
108 $criteriaInstance->setLimit(1);
110 // Get a search result
111 $result = $this->doSelectByCriteria($criteriaInstance);
114 if ($result->next()) {
116 $this->cacheInstance->add($userName, $result->getResultArray());
118 // Entry found, so all is fine