dbf872257336d391c223742f7ee984f5e5579cf2
[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\Manager\Login\ManageableMember;
10 use Org\Mxchange\CoreFramework\Registry\Registerable;
11 use Org\Mxchange\CoreFramework\User\BaseUser;
12 use Org\Mxchange\CoreFramework\User\UsernameMissingException;
13
14 /**
15  * A generic class for handling users
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
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.
27  *
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.
32  *
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/>.
35  */
36 class Member extends BaseUser implements ManageableMember, Registerable {
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         private function __construct () {
43                 // Call parent constructor
44                 parent::__construct(__CLASS__);
45         }
46
47         /**
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.
51          *
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'
56          */
57         public static final function createMemberByUsername ($userName) {
58                 // Get a new instance
59                 $userInstance = new Member();
60
61                 // Set the username
62                 $userInstance->setUserName($userName);
63
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);
71                 }
72
73                 // Return the instance
74                 return $userInstance;
75         }
76
77         /**
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.
80          *
81          * @param       $email                  Email address of the user
82          * @return      $userInstance   An instance of this user class
83          */
84         public static final function createMemberByEmail ($email) {
85                 // Get a new instance
86                 $userInstance = new Member();
87
88                 // Set the username
89                 $userInstance->setEmail($email);
90
91                 // Return the instance
92                 return $userInstance;
93         }
94
95         /**
96          * Creates a user by a given request instance
97          *
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
101          */
102         public static final function createMemberByRequest (Requestable $requestInstance) {
103                 // Determine if by email or username
104                 if (!is_null($requestInstance->getRequestElement('username'))) {
105                         // Username supplied
106                         $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
107                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
108                         // Email supplied
109                         $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
110                 } else {
111                         // Unsupported mode
112                         $userInstance = new Member();
113                         $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
114                         exit();
115                 }
116
117                 // Return the prepared instance
118                 return $userInstance;
119         }
120
121         /**
122          * Updates the last activity timestamp and last performed action in the
123          * database result.
124          *
125          * @param       $requestInstance        A requestable class instance
126          * @return      void
127          */
128         public function updateLastActivity (Requestable $requestInstance) {
129                 // Set last action
130                 $lastAction = $requestInstance->getRequestElement('action');
131
132                 // If there is no action use the default on
133                 if (is_null($lastAction)) {
134                         $lastAction = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('login_default_action');
135                 } // END - if
136
137                 // Get a critieria instance
138                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
139
140                 // Add search criteria
141                 $searchInstance->addCriteria(UserDatabaseFrontend::DB_COLUMN_USERNAME, $this->getUserName());
142                 $searchInstance->setLimit(1);
143
144                 // Now get another criteria
145                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
146
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);
150
151                 // Add the search criteria for searching for the right entry
152                 $updateInstance->setSearchInstance($searchInstance);
153
154                 // Set frontend class name
155                 $updateInstance->setFrontendConfigEntry('user_db_frontend_class');
156
157                 // Remember the update in database result
158                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
159         }
160
161 }