3 namespace CoreFramework\User\Guest;
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
7 use CoreFramework\Request\Requestable;
8 use CoreFramework\User\BaseUser;
11 * A generic class for handling guests
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class Guest extends BaseUser implements ManageableGuest, Registerable {
34 const EXCEPTION_USERNAME_NOT_FOUND = 0x170;
35 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x171;
36 const EXCEPTION_USER_PASS_MISMATCH = 0x172;
37 const EXCEPTION_USER_NOT_GUEST = 0x173;
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
50 * Creates a user by a given request instance
52 * @param $requestInstance An instance of a Requestable class
53 * @return $userInstance An instance of this user class
54 * @todo Add more ways over creating user classes
56 public static final function createGuestByRequest (Requestable $requestInstance) {
57 // Determine if by email or username
58 if (!is_null($requestInstance->getRequestElement('username'))) {
60 $userInstance = self::createGuestByUserName($requestInstance->getRequestElement('username'));
61 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
63 $userInstance = self::createGuestByEmail($requestInstance->getRequestElement('email'));
66 $userInstance = new Guest();
67 $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
71 // Return the prepared instance
76 * Creates an instance of this user class by a provided username. This
77 * factory method will check if username is already taken and if not so it
78 * will throw an exception.
80 * @param $userName Username we need a class instance for
81 * @return $userInstance An instance of this user class
82 * @throws UsernameMissingException If the username does not exist
83 * @throws UserNoGuestException If the user is no guest account
85 public static final function createGuestByUsername ($userName) {
87 $userInstance = new Guest();
90 $userInstance->setUserName($userName);
92 // Check if username exists
93 if ($userInstance->ifUsernameExists() === FALSE) {
94 // Throw an exception here
95 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
96 } elseif ($userInstance->isGuest() === FALSE) {
97 // Sanity check on 'guest' status failed
98 throw new UserNoGuestException(array($userInstance, $userName), self::EXCEPTION_USER_NOT_GUEST_STATUS);
101 // Return the instance
102 return $userInstance;
106 * Creates an instance of this user class by a provided email address. This
107 * factory method will not check if email address is there.
109 * @param $email Email address of the user
110 * @return $userInstance An instance of this user class
112 public static final function createGuestByEmail ($email) {
113 // Get a new instance
114 $userInstance = new Guest();
117 $userInstance->setEmail($email);
119 // Return the instance
120 return $userInstance;
124 * Updates the last activity timestamp and last performed action in the
125 * database result. You should call flushPendingUpdates() to flush these updates
126 * to the database layer.
128 * @param $requestInstance A requestable class instance
131 public function updateLastActivity (Requestable $requestInstance) {
132 // No activity will be logged for guest accounts
136 * Flushs all pending updates to the database layer
140 public function flushPendingUpdates () {
141 // No updates will be flushed to database!