3 namespace Org\Mxchange\CoreFramework\User\Login;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Database\Frontend\User\UserDatabaseWrapper;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Manager\Login\ManageableMember;
9 use Org\Mxchange\CoreFramework\Registry\Registerable;
10 use Org\Mxchange\CoreFramework\User\BaseUser;
11 use Org\Mxchange\CoreFramework\User\UsernameMissingException;
14 * A generic class for handling users
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class Member extends BaseUser implements ManageableMember, Registerable {
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
47 * Creates an instance of this user class by a provided username. This
48 * factory method will check if username is already taken and if not so it
49 * will throw an exception.
51 * @param $userName Username we need a class instance for
52 * @return $userInstance An instance of this user class
53 * @throws UsernameMissingException If the username does not exist
54 * @throws UnexpectedGuestAccountException If the user status is 'guest'
56 public static final function createMemberByUsername ($userName) {
58 $userInstance = new Member();
61 $userInstance->setUserName($userName);
63 // Check if username exists
64 if ($userInstance->ifUsernameExists() === false) {
65 // Throw an exception here
66 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
67 } elseif ($userInstance->isGuest() === true) {
68 // User should not be a guest here
69 throw new UnexpectedGuestAccountException(array($userInstance, $userName), self::EXCEPTION_USER_IS_GUEST);
72 // Return the instance
77 * Creates an instance of this user class by a provided email address. This
78 * factory method will not check if email address is there.
80 * @param $email Email address of the user
81 * @return $userInstance An instance of this user class
83 public static final function createMemberByEmail ($email) {
85 $userInstance = new Member();
88 $userInstance->setEmail($email);
90 // Return the instance
95 * Creates a user by a given request instance
97 * @param $requestInstance An instance of a Requestable class
98 * @return $userInstance An instance of this user class
99 * @todo Add more ways over creating user classes
101 public static final function createMemberByRequest (Requestable $requestInstance) {
102 // Determine if by email or username
103 if (!is_null($requestInstance->getRequestElement('username'))) {
105 $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
106 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
108 $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
111 $userInstance = new Member();
112 $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
116 // Return the prepared instance
117 return $userInstance;
121 * Updates the last activity timestamp and last performed action in the
122 * database result. You should call flushPendingUpdates() to flush these updates
123 * to the database layer.
125 * @param $requestInstance A requestable class instance
128 public function updateLastActivity (Requestable $requestInstance) {
130 $lastAction = $requestInstance->getRequestElement('action');
132 // If there is no action use the default on
133 if (is_null($lastAction)) {
134 $lastAction = $this->getConfigInstance()->getConfigEntry('login_default_action');
137 // Get a critieria instance
138 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
140 // Add search criteria
141 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
142 $searchInstance->setLimit(1);
144 // Now get another criteria
145 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
147 // And add our both entries
148 $updateInstance->addCriteria('last_activity', date('Y-m-d H:i:s', time()));
149 $updateInstance->addCriteria('last_action', $lastAction);
151 // Add the search criteria for searching for the right entry
152 $updateInstance->setSearchInstance($searchInstance);
154 // Set wrapper class name
155 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
157 // Remember the update in database result
158 $this->getResultInstance()->add2UpdateQueue($updateInstance);