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\UserDatabaseWrapper;
8 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Manager\Login\ManageableMember;
10 use Org\Mxchange\CoreFramework\Registry\Registerable;
11 use Org\Mxchange\CoreFramework\User\BaseUser;
12 use Org\Mxchange\CoreFramework\User\UsernameMissingException;
15 * A generic class for handling users
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class Member extends BaseUser implements ManageableMember, Registerable {
38 * Protected constructor
42 protected function __construct () {
43 // Call parent constructor
44 parent::__construct(__CLASS__);
48 * Creates an instance of this user class by a provided username. This
49 * factory method will check if username is already taken and if not so it
50 * will throw an exception.
52 * @param $userName Username we need a class instance for
53 * @return $userInstance An instance of this user class
54 * @throws UsernameMissingException If the username does not exist
55 * @throws UnexpectedGuestAccountException If the user status is 'guest'
57 public static final function createMemberByUsername ($userName) {
59 $userInstance = new Member();
62 $userInstance->setUserName($userName);
64 // Check if username exists
65 if ($userInstance->ifUsernameExists() === false) {
66 // Throw an exception here
67 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
68 } elseif ($userInstance->isGuest() === true) {
69 // User should not be a guest here
70 throw new UnexpectedGuestAccountException(array($userInstance, $userName), self::EXCEPTION_USER_IS_GUEST);
73 // Return the instance
78 * Creates an instance of this user class by a provided email address. This
79 * factory method will not check if email address is there.
81 * @param $email Email address of the user
82 * @return $userInstance An instance of this user class
84 public static final function createMemberByEmail ($email) {
86 $userInstance = new Member();
89 $userInstance->setEmail($email);
91 // Return the instance
96 * Creates a user by a given request instance
98 * @param $requestInstance An instance of a Requestable class
99 * @return $userInstance An instance of this user class
100 * @todo Add more ways over creating user classes
102 public static final function createMemberByRequest (Requestable $requestInstance) {
103 // Determine if by email or username
104 if (!is_null($requestInstance->getRequestElement('username'))) {
106 $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
107 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
109 $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
112 $userInstance = new Member();
113 $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
117 // Return the prepared instance
118 return $userInstance;
122 * Updates the last activity timestamp and last performed action in the
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 = FrameworkBootstrap::getConfigurationInstance()->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);