3 namespace Org\Mxchange\CoreFramework\User\Guest;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Manager\Guest\ManageableGuest;
7 use Org\Mxchange\CoreFramework\Registry\Registerable;
8 use Org\Mxchange\CoreFramework\Request\Requestable;
9 use Org\Mxchange\CoreFramework\User\BaseUser;
10 use Org\Mxchange\CoreFramework\User\UsernameMissingException;
13 * A generic class for handling guests
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class Guest extends BaseUser implements ManageableGuest, Registerable {
36 const EXCEPTION_USERNAME_NOT_FOUND = 0x170;
37 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x171;
38 const EXCEPTION_USER_PASS_MISMATCH = 0x172;
39 const EXCEPTION_USER_NOT_GUEST = 0x173;
42 * Protected constructor
46 protected function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
52 * Creates a user by a given request instance
54 * @param $requestInstance An instance of a Requestable class
55 * @return $userInstance An instance of this user class
56 * @todo Add more ways over creating user classes
58 public static final function createGuestByRequest (Requestable $requestInstance) {
59 // Determine if by email or username
60 if (!is_null($requestInstance->getRequestElement('username'))) {
62 $userInstance = self::createGuestByUserName($requestInstance->getRequestElement('username'));
63 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
65 $userInstance = self::createGuestByEmail($requestInstance->getRequestElement('email'));
68 $userInstance = new Guest();
69 $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
73 // Return the prepared instance
78 * Creates an instance of this user class by a provided username. This
79 * factory method will check if username is already taken and if not so it
80 * will throw an exception.
82 * @param $userName Username we need a class instance for
83 * @return $userInstance An instance of this user class
84 * @throws UsernameMissingException If the username does not exist
85 * @throws UserNoGuestException If the user is no guest account
87 public static final function createGuestByUsername ($userName) {
89 $userInstance = new Guest();
92 $userInstance->setUserName($userName);
94 // Check if username exists
95 if ($userInstance->ifUsernameExists() === false) {
96 // Throw an exception here
97 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
98 } elseif ($userInstance->isGuest() === false) {
99 // Sanity check on 'guest' status failed
100 throw new UserNoGuestException(array($userInstance, $userName), self::EXCEPTION_USER_NOT_GUEST_STATUS);
103 // Return the instance
104 return $userInstance;
108 * Creates an instance of this user class by a provided email address. This
109 * factory method will not check if email address is there.
111 * @param $email Email address of the user
112 * @return $userInstance An instance of this user class
114 public static final function createGuestByEmail ($email) {
115 // Get a new instance
116 $userInstance = new Guest();
119 $userInstance->setEmail($email);
121 // Return the instance
122 return $userInstance;
126 * Updates the last activity timestamp and last performed action in the
127 * database result. You should call flushPendingUpdates() to flush these updates
128 * to the database layer.
130 * @param $requestInstance A requestable class instance
133 public function updateLastActivity (Requestable $requestInstance) {
134 // No activity will be logged for guest accounts
138 * Flushs all pending updates to the database layer
142 public function flushPendingUpdates () {
143 // No updates will be flushed to database!