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