]> git.mxchange.org Git - core.git/blob - framework/main/classes/user/member/class_Member.php
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\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;
12
13 /**
14  * A generic class for handling users
15  *
16  * @author              Roland Haeder <webmaster@shipsimu.org>
17  * @version             0.0.0
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
21  *
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.
26  *
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.
31  *
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/>.
34  */
35 class Member extends BaseUser implements ManageableMember, Registerable {
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
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.
50          *
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'
55          */
56         public static final function createMemberByUsername ($userName) {
57                 // Get a new instance
58                 $userInstance = new Member();
59
60                 // Set the username
61                 $userInstance->setUserName($userName);
62
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);
70                 }
71
72                 // Return the instance
73                 return $userInstance;
74         }
75
76         /**
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.
79          *
80          * @param       $email                  Email address of the user
81          * @return      $userInstance   An instance of this user class
82          */
83         public static final function createMemberByEmail ($email) {
84                 // Get a new instance
85                 $userInstance = new Member();
86
87                 // Set the username
88                 $userInstance->setEmail($email);
89
90                 // Return the instance
91                 return $userInstance;
92         }
93
94         /**
95          * Creates a user by a given request instance
96          *
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
100          */
101         public static final function createMemberByRequest (Requestable $requestInstance) {
102                 // Determine if by email or username
103                 if (!is_null($requestInstance->getRequestElement('username'))) {
104                         // Username supplied
105                         $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
106                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
107                         // Email supplied
108                         $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
109                 } else {
110                         // Unsupported mode
111                         $userInstance = new Member();
112                         $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
113                         exit();
114                 }
115
116                 // Return the prepared instance
117                 return $userInstance;
118         }
119
120         /**
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.
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 = $this->getConfigInstance()->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(UserDatabaseWrapper::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 wrapper class name
155                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
156
157                 // Remember the update in database result
158                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
159         }
160
161 }