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