Continued:
[core.git] / framework / main / classes / user / member / class_Member.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\User\Login;
4
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;
14
15 // Import SPL stuff
16 use \InvalidArgumentException;
17
18 /**
19  * A generic class for handling users
20  *
21  * @author              Roland Haeder <webmaster@shipsimu.org>
22  * @version             0.0.0
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
26  *
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.
31  *
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.
36  *
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/>.
39  */
40 class Member extends BaseUser implements ManageableMember, Registerable {
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         private function __construct () {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49         }
50
51         /**
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.
55          *
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'
60          */
61         public static final function createMemberByUsername (string $userName) {
62                 // Check parameter
63                 if (empty($userName)) {
64                         // Throw IAE
65                         throw new InvalidArgumentException('Parameter "userName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
66                 }
67
68                 // Get a new instance
69                 $userInstance = new Member();
70
71                 // Set the username
72                 $userInstance->setUserName($userName);
73
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);
81                 }
82
83                 // Return the instance
84                 return $userInstance;
85         }
86
87         /**
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.
90          *
91          * @param       $email                  Email address of the user
92          * @return      $userInstance   An instance of this user class
93          */
94         public static final function createMemberByEmail (string $email) {
95                 // Check parameter
96                 if (empty($email)) {
97                         // Throw IAE
98                         throw new InvalidArgumentException('Parameter "email" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
99                 }
100
101                 // Get a new instance
102                 $userInstance = new Member();
103
104                 // Set the username
105                 $userInstance->setEmail($email);
106
107                 // Return the instance
108                 return $userInstance;
109         }
110
111         /**
112          * Creates a user by a given request instance
113          *
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
117          */
118         public static final function createMemberByRequest (Requestable $requestInstance) {
119                 // Determine if by email or username
120                 if (!is_null($requestInstance->getRequestElement('username'))) {
121                         // Username supplied
122                         $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
123                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
124                         // Email supplied
125                         $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
126                 } else {
127                         // Unsupported mode
128                         $userInstance = new Member();
129                         $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
130                         exit();
131                 }
132
133                 // Return the prepared instance
134                 return $userInstance;
135         }
136
137         /**
138          * Updates the last activity timestamp and last performed action in the
139          * database result.
140          *
141          * @param       $requestInstance        A requestable class instance
142          * @return      void
143          */
144         public function updateLastActivity (Requestable $requestInstance) {
145                 // Set last action
146                 $lastAction = $requestInstance->getRequestElement('action');
147
148                 // If there is no action use the default on
149                 if (is_null($lastAction)) {
150                         $lastAction = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('login_default_action');
151                 }
152
153                 // Get a critieria instance
154                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
155
156                 // Add search criteria
157                 $searchInstance->addCriteria(UserDatabaseFrontend::DB_COLUMN_USERNAME, $this->getUserName());
158                 $searchInstance->setLimit(1);
159
160                 // Now get another criteria
161                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
162
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);
166
167                 // Add the search criteria for searching for the right entry
168                 $updateInstance->setSearchInstance($searchInstance);
169
170                 // Set frontend class name
171                 $updateInstance->setFrontendConfigEntry('user_db_frontend_class');
172
173                 // Remember the update in database result
174                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
175         }
176
177 }