3 namespace Org\Mxchange\CoreFramework\User\Login;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Database\Frontend\User\UserDatabaseFrontend;
8 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
10 use Org\Mxchange\CoreFramework\Manager\Login\ManageableMember;
11 use Org\Mxchange\CoreFramework\Registry\Registerable;
12 use Org\Mxchange\CoreFramework\User\BaseUser;
13 use Org\Mxchange\CoreFramework\User\UsernameMissingException;
16 use \InvalidArgumentException;
19 * A generic class for handling users
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 class Member extends BaseUser implements ManageableMember, Registerable {
42 * Protected constructor
46 private function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
52 * Creates an instance of this user class by a provided username. This
53 * factory method will check if username is already taken and if not so it
54 * will throw an exception.
56 * @param $userName Username we need a class instance for
57 * @return $userInstance An instance of this user class
58 * @throws UsernameMissingException If the username does not exist
59 * @throws UnexpectedGuestAccountException If the user status is 'guest'
61 public static final function createMemberByUsername (string $userName) {
63 if (empty($userName)) {
65 throw new InvalidArgumentException('Parameter "userName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
69 $userInstance = new Member();
72 $userInstance->setUserName($userName);
74 // Check if username exists
75 if ($userInstance->ifUsernameExists() === false) {
76 // Throw an exception here
77 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
78 } elseif ($userInstance->isGuest() === true) {
79 // User should not be a guest here
80 throw new UnexpectedGuestAccountException(array($userInstance, $userName), self::EXCEPTION_USER_IS_GUEST);
83 // Return the instance
88 * Creates an instance of this user class by a provided email address. This
89 * factory method will not check if email address is there.
91 * @param $email Email address of the user
92 * @return $userInstance An instance of this user class
94 public static final function createMemberByEmail (string $email) {
98 throw new InvalidArgumentException('Parameter "email" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
101 // Get a new instance
102 $userInstance = new Member();
105 $userInstance->setEmail($email);
107 // Return the instance
108 return $userInstance;
112 * Creates a user by a given request instance
114 * @param $requestInstance An instance of a Requestable class
115 * @return $userInstance An instance of this user class
116 * @todo Add more ways over creating user classes
118 public static final function createMemberByRequest (Requestable $requestInstance) {
119 // Determine if by email or username
120 if (!is_null($requestInstance->getRequestElement('username'))) {
122 $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
123 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
125 $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
128 $userInstance = new Member();
129 $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
133 // Return the prepared instance
134 return $userInstance;
138 * Updates the last activity timestamp and last performed action in the
141 * @param $requestInstance A requestable class instance
144 public function updateLastActivity (Requestable $requestInstance) {
146 $lastAction = $requestInstance->getRequestElement('action');
148 // If there is no action use the default on
149 if (is_null($lastAction)) {
150 $lastAction = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('login_default_action');
153 // Get a critieria instance
154 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
156 // Add search criteria
157 $searchInstance->addCriteria(UserDatabaseFrontend::DB_COLUMN_USERNAME, $this->getUserName());
158 $searchInstance->setLimit(1);
160 // Now get another criteria
161 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
163 // And add our both entries
164 $updateInstance->addCriteria('last_activity', date('Y-m-d H:i:s', time()));
165 $updateInstance->addCriteria('last_action', $lastAction);
167 // Add the search criteria for searching for the right entry
168 $updateInstance->setSearchInstance($searchInstance);
170 // Set frontend class name
171 $updateInstance->setFrontendConfigEntry('user_db_frontend_class');
173 // Remember the update in database result
174 $this->getResultInstance()->add2UpdateQueue($updateInstance);