]> git.mxchange.org Git - core.git/blob - framework/main/classes/user/guest/class_Guest.php
Some updates:
[core.git] / framework / main / classes / user / guest / class_Guest.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\User\Guest;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Manager\Guest\ManageableGuest;
7 use Org\Mxchange\CoreFramework\Registry\Registerable;
8 use Org\Mxchange\CoreFramework\Request\Requestable;
9 use Org\Mxchange\CoreFramework\User\BaseUser;
10
11 /**
12  * A generic class for handling guests
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
16 <<<<<<< HEAD:framework/main/classes/user/guest/class_Guest.php
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 =======
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
20 >>>>>>> Some updates::inc/main/classes/user/guest/class_Guest.php
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.shipsimu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program. If not, see <http://www.gnu.org/licenses/>.
36  */
37 class Guest extends BaseUser implements ManageableGuest, Registerable {
38         // Exceptions
39         const EXCEPTION_USERNAME_NOT_FOUND   = 0x170;
40         const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x171;
41         const EXCEPTION_USER_PASS_MISMATCH   = 0x172;
42         const EXCEPTION_USER_NOT_GUEST       = 0x173;
43
44         /**
45          * Protected constructor
46          *
47          * @return      void
48          */
49         protected function __construct () {
50                 // Call parent constructor
51                 parent::__construct(__CLASS__);
52         }
53
54         /**
55          * Creates a user by a given request instance
56          *
57          * @param       $requestInstance        An instance of a Requestable class
58          * @return      $userInstance           An instance of this user class
59          * @todo        Add more ways over creating user classes
60          */
61         public static final function createGuestByRequest (Requestable $requestInstance) {
62                 // Determine if by email or username
63                 if (!is_null($requestInstance->getRequestElement('username'))) {
64                         // Username supplied
65                         $userInstance = self::createGuestByUserName($requestInstance->getRequestElement('username'));
66                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
67                         // Email supplied
68                         $userInstance = self::createGuestByEmail($requestInstance->getRequestElement('email'));
69                 } else {
70                         // Unsupported mode
71                         $userInstance = new Guest();
72                         $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
73                         exit();
74                 }
75
76                 // Return the prepared instance
77                 return $userInstance;
78         }
79
80         /**
81          * Creates an instance of this user class by a provided username. This
82          * factory method will check if username is already taken and if not so it
83          * will throw an exception.
84          *
85          * @param       $userName               Username we need a class instance for
86          * @return      $userInstance   An instance of this user class
87          * @throws      UsernameMissingException        If the username does not exist
88          * @throws      UserNoGuestException            If the user is no guest account
89          */
90         public static final function createGuestByUsername ($userName) {
91                 // Get a new instance
92                 $userInstance = new Guest();
93
94                 // Set the username
95                 $userInstance->setUserName($userName);
96
97                 // Check if username exists
98                 if ($userInstance->ifUsernameExists() === false) {
99                         // Throw an exception here
100                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
101                 } elseif ($userInstance->isGuest() === false) {
102                         // Sanity check on 'guest' status failed
103                         throw new UserNoGuestException(array($userInstance, $userName), self::EXCEPTION_USER_NOT_GUEST_STATUS);
104                 }
105
106                 // Return the instance
107                 return $userInstance;
108         }
109
110         /**
111          * Creates an instance of this user class by a provided email address. This
112          * factory method will not check if email address is there.
113          *
114          * @param       $email                  Email address of the user
115          * @return      $userInstance   An instance of this user class
116          */
117         public static final function createGuestByEmail ($email) {
118                 // Get a new instance
119                 $userInstance = new Guest();
120
121                 // Set the username
122                 $userInstance->setEmail($email);
123
124                 // Return the instance
125                 return $userInstance;
126         }
127
128         /**
129          * Updates the last activity timestamp and last performed action in the
130          * database result. You should call flushPendingUpdates() to flush these updates
131          * to the database layer.
132          *
133          * @param       $requestInstance        A requestable class instance
134          * @return      void
135          */
136         public function updateLastActivity (Requestable $requestInstance) {
137                 // No activity will be logged for guest accounts
138         }
139
140         /**
141          * Flushs all pending updates to the database layer
142          *
143          * @return      void
144          */
145         public function flushPendingUpdates () {
146                 // No updates will be flushed to database!
147         }
148
149 }