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 use \InvalidArgumentException;
16 * A generic class for handling guests
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class Guest extends BaseUser implements ManageableGuest, Registerable {
39 const EXCEPTION_USERNAME_NOT_FOUND = 0x170;
40 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x171;
41 const EXCEPTION_USER_PASS_MISMATCH = 0x172;
42 const EXCEPTION_USER_NOT_GUEST = 0x173;
45 * Protected constructor
49 private function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
55 * Creates a user by a given request instance
57 * @param $requestInstance An instance of a Requestable class
58 * @return $userInstance An instance of this user class
59 * @todo Add more ways over creating user classes
61 public static final function createGuestByRequest (Requestable $requestInstance) {
62 // Determine if by email or username
63 if (!is_null($requestInstance->getRequestElement('username'))) {
65 $userInstance = self::createGuestByUserName($requestInstance->getRequestElement('username'));
66 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
68 $userInstance = self::createGuestByEmail($requestInstance->getRequestElement('email'));
71 $userInstance = new Guest();
72 $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
76 // Return the prepared instance
81 * Creates an instance of this user class by a provided username. This
82 * factory method will check if username is already taken and if not so it
83 * will throw an exception.
85 * @param $userName Username we need a class instance for
86 * @return $userInstance An instance of this user class
87 * @throws UsernameMissingException If the username does not exist
88 * @throws UserNoGuestException If the user is no guest account
90 public static final function createGuestByUsername (string $userName) {
92 if (empty($userName)) {
94 throw new InvalidArgumentException('Paramter "userName" is empty');
98 $userInstance = new Guest();
101 $userInstance->setUserName($userName);
103 // Check if username exists
104 if ($userInstance->ifUsernameExists() === false) {
105 // Throw an exception here
106 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
107 } elseif ($userInstance->isGuest() === false) {
108 // Sanity check on 'guest' status failed
109 throw new UserNoGuestException(array($userInstance, $userName), self::EXCEPTION_USER_NOT_GUEST_STATUS);
112 // Return the instance
113 return $userInstance;
117 * Creates an instance of this user class by a provided email address. This
118 * factory method will not check if email address is there.
120 * @param $email Email address of the user
121 * @return $userInstance An instance of this user class
123 public static final function createGuestByEmail (string $email) {
127 throw new InvalidArgumentException('Paramter "email" is empty');
130 // Get a new instance
131 $userInstance = new Guest();
134 $userInstance->setEmail($email);
136 // Return the instance
137 return $userInstance;
141 * Updates the last activity timestamp and last performed action in the
144 * @param $requestInstance A requestable class instance
147 public function updateLastActivity (Requestable $requestInstance) {
148 // No activity will be logged for guest accounts