6be881617771393ed848f5137050bc56672da0d3
[core.git] / inc / main / classes / user / member / class_Member.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\User\Login;
4
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
7
8 /**
9  * A generic class for handling users
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class Member extends BaseUser implements ManageableMember, Registerable {
31         /**
32          * Protected constructor
33          *
34          * @return      void
35          */
36         protected function __construct () {
37                 // Call parent constructor
38                 parent::__construct(__CLASS__);
39         }
40
41         /**
42          * Creates an instance of this user class by a provided username. This
43          * factory method will check if username is already taken and if not so it
44          * will throw an exception.
45          *
46          * @param       $userName               Username we need a class instance for
47          * @return      $userInstance   An instance of this user class
48          * @throws      UsernameMissingException        If the username does not exist
49          * @throws      UnexpectedGuestAccountException         If the user status is 'guest'
50          */
51         public static final function createMemberByUsername ($userName) {
52                 // Get a new instance
53                 $userInstance = new Member();
54
55                 // Set the username
56                 $userInstance->setUserName($userName);
57
58                 // Check if username exists
59                 if ($userInstance->ifUsernameExists() === FALSE) {
60                         // Throw an exception here
61                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
62                 } elseif ($userInstance->isGuest() === TRUE) {
63                         // User should not be a guest here
64                         throw new UnexpectedGuestAccountException(array($userInstance, $userName), self::EXCEPTION_USER_IS_GUEST);
65                 }
66
67                 // Return the instance
68                 return $userInstance;
69         }
70
71         /**
72          * Creates an instance of this user class by a provided email address. This
73          * factory method will not check if email address is there.
74          *
75          * @param       $email                  Email address of the user
76          * @return      $userInstance   An instance of this user class
77          */
78         public static final function createMemberByEmail ($email) {
79                 // Get a new instance
80                 $userInstance = new Member();
81
82                 // Set the username
83                 $userInstance->setEmail($email);
84
85                 // Return the instance
86                 return $userInstance;
87         }
88
89         /**
90          * Creates a user by a given request instance
91          *
92          * @param       $requestInstance        An instance of a Requestable class
93          * @return      $userInstance           An instance of this user class
94          * @todo        Add more ways over creating user classes
95          */
96         public static final function createMemberByRequest (Requestable $requestInstance) {
97                 // Determine if by email or username
98                 if (!is_null($requestInstance->getRequestElement('username'))) {
99                         // Username supplied
100                         $userInstance = self::createMemberByUserName($requestInstance->getRequestElement('username'));
101                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
102                         // Email supplied
103                         $userInstance = self::createMemberByEmail($requestInstance->getRequestElement('email'));
104                 } else {
105                         // Unsupported mode
106                         $userInstance = new Member();
107                         $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
108                         exit();
109                 }
110
111                 // Return the prepared instance
112                 return $userInstance;
113         }
114
115         /**
116          * Updates the last activity timestamp and last performed action in the
117          * database result. You should call flushPendingUpdates() to flush these updates
118          * to the database layer.
119          *
120          * @param       $requestInstance        A requestable class instance
121          * @return      void
122          */
123         public function updateLastActivity (Requestable $requestInstance) {
124                 // Set last action
125                 $lastAction = $requestInstance->getRequestElement('action');
126
127                 // If there is no action use the default on
128                 if (is_null($lastAction)) {
129                         $lastAction = $this->getConfigInstance()->getConfigEntry('login_default_action');
130                 } // END - if
131
132                 // Get a critieria instance
133                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
134
135                 // Add search criteria
136                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
137                 $searchInstance->setLimit(1);
138
139                 // Now get another criteria
140                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
141
142                 // And add our both entries
143                 $updateInstance->addCriteria('last_activity', date('Y-m-d H:i:s', time()));
144                 $updateInstance->addCriteria('last_action', $lastAction);
145
146                 // Add the search criteria for searching for the right entry
147                 $updateInstance->setSearchInstance($searchInstance);
148
149                 // Set wrapper class name
150                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
151
152                 // Remember the update in database result
153                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
154         }
155
156 }