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