Continued with renaming-season:
[core.git] / framework / main / classes / user / guest / class_Guest.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\User\Guest;
4
5 // Import framework stuff
6 use CoreFramework\Registry\Registerable;
7 use CoreFramework\Request\Requestable;
8
9 /**
10  * A generic class for handling guests
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 Guest extends BaseUser implements ManageableGuest, Registerable {
32         // Exceptions
33         const EXCEPTION_USERNAME_NOT_FOUND   = 0x170;
34         const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x171;
35         const EXCEPTION_USER_PASS_MISMATCH   = 0x172;
36         const EXCEPTION_USER_NOT_GUEST       = 0x173;
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Creates a user by a given request instance
50          *
51          * @param       $requestInstance        An instance of a Requestable class
52          * @return      $userInstance           An instance of this user class
53          * @todo        Add more ways over creating user classes
54          */
55         public static final function createGuestByRequest (Requestable $requestInstance) {
56                 // Determine if by email or username
57                 if (!is_null($requestInstance->getRequestElement('username'))) {
58                         // Username supplied
59                         $userInstance = self::createGuestByUserName($requestInstance->getRequestElement('username'));
60                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
61                         // Email supplied
62                         $userInstance = self::createGuestByEmail($requestInstance->getRequestElement('email'));
63                 } else {
64                         // Unsupported mode
65                         $userInstance = new Guest();
66                         $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
67                         exit();
68                 }
69
70                 // Return the prepared instance
71                 return $userInstance;
72         }
73
74         /**
75          * Creates an instance of this user class by a provided username. This
76          * factory method will check if username is already taken and if not so it
77          * will throw an exception.
78          *
79          * @param       $userName               Username we need a class instance for
80          * @return      $userInstance   An instance of this user class
81          * @throws      UsernameMissingException        If the username does not exist
82          * @throws      UserNoGuestException            If the user is no guest account
83          */
84         public static final function createGuestByUsername ($userName) {
85                 // Get a new instance
86                 $userInstance = new Guest();
87
88                 // Set the username
89                 $userInstance->setUserName($userName);
90
91                 // Check if username exists
92                 if ($userInstance->ifUsernameExists() === FALSE) {
93                         // Throw an exception here
94                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
95                 } elseif ($userInstance->isGuest() === FALSE) {
96                         // Sanity check on 'guest' status failed
97                         throw new UserNoGuestException(array($userInstance, $userName), self::EXCEPTION_USER_NOT_GUEST_STATUS);
98                 }
99
100                 // Return the instance
101                 return $userInstance;
102         }
103
104         /**
105          * Creates an instance of this user class by a provided email address. This
106          * factory method will not check if email address is there.
107          *
108          * @param       $email                  Email address of the user
109          * @return      $userInstance   An instance of this user class
110          */
111         public static final function createGuestByEmail ($email) {
112                 // Get a new instance
113                 $userInstance = new Guest();
114
115                 // Set the username
116                 $userInstance->setEmail($email);
117
118                 // Return the instance
119                 return $userInstance;
120         }
121
122         /**
123          * Updates the last activity timestamp and last performed action in the
124          * database result. You should call flushPendingUpdates() to flush these updates
125          * to the database layer.
126          *
127          * @param       $requestInstance        A requestable class instance
128          * @return      void
129          */
130         public function updateLastActivity (Requestable $requestInstance) {
131                 // No activity will be logged for guest accounts
132         }
133
134         /**
135          * Flushs all pending updates to the database layer
136          *
137          * @return      void
138          */
139         public function flushPendingUpdates () {
140                 // No updates will be flushed to database!
141         }
142
143 }