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