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