3 namespace Org\Mxchange\CoreFramework\User\Guest;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
7 use Org\Mxchange\CoreFramework\Manager\Guest\ManageableGuest;
8 use Org\Mxchange\CoreFramework\Registry\Registerable;
9 use Org\Mxchange\CoreFramework\Request\Requestable;
10 use Org\Mxchange\CoreFramework\User\BaseUser;
11 use Org\Mxchange\CoreFramework\User\UsernameMissingException;
14 use \InvalidArgumentException;
17 * A generic class for handling guests
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class Guest extends BaseUser implements ManageableGuest, Registerable {
40 const EXCEPTION_USERNAME_NOT_FOUND = 0x170;
41 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x171;
42 const EXCEPTION_USER_PASS_MISMATCH = 0x172;
43 const EXCEPTION_USER_NOT_GUEST = 0x173;
46 * Protected constructor
50 private function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Creates a user by a given request instance
58 * @param $requestInstance An instance of a Requestable class
59 * @return $userInstance An instance of this user class
60 * @todo Add more ways over creating user classes
62 public static final function createGuestByRequest (Requestable $requestInstance) {
63 // Determine if by email or username
64 if (!is_null($requestInstance->getRequestElement('username'))) {
66 $userInstance = self::createGuestByUserName($requestInstance->getRequestElement('username'));
67 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
69 $userInstance = self::createGuestByEmail($requestInstance->getRequestElement('email'));
72 $userInstance = new Guest();
73 $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
77 // Return the prepared instance
82 * Creates an instance of this user class by a provided username. This
83 * factory method will check if username is already taken and if not so it
84 * will throw an exception.
86 * @param $userName Username we need a class instance for
87 * @return $userInstance An instance of this user class
88 * @throws UsernameMissingException If the username does not exist
89 * @throws UserNoGuestException If the user is no guest account
91 public static final function createGuestByUsername (string $userName) {
93 if (empty($userName)) {
95 throw new InvalidArgumentException('Paramter "userName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
99 $userInstance = new Guest();
102 $userInstance->setUserName($userName);
104 // Check if username exists
105 if ($userInstance->ifUsernameExists() === false) {
106 // Throw an exception here
107 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
108 } elseif ($userInstance->isGuest() === false) {
109 // Sanity check on 'guest' status failed
110 throw new UserNoGuestException(array($userInstance, $userName), self::EXCEPTION_USER_NOT_GUEST_STATUS);
113 // Return the instance
114 return $userInstance;
118 * Creates an instance of this user class by a provided email address. This
119 * factory method will not check if email address is there.
121 * @param $email Email address of the user
122 * @return $userInstance An instance of this user class
124 public static final function createGuestByEmail (string $email) {
128 throw new InvalidArgumentException('Paramter "email" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
131 // Get a new instance
132 $userInstance = new Guest();
135 $userInstance->setEmail($email);
137 // Return the instance
138 return $userInstance;
142 * Updates the last activity timestamp and last performed action in the
145 * @param $requestInstance A requestable class instance
148 public function updateLastActivity (Requestable $requestInstance) {
149 // No activity will be logged for guest accounts