Continued:
[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 use CoreFramework\User\BaseUser;
9
10 /**
11  * A generic class for handling guests
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 Guest extends BaseUser implements ManageableGuest, Registerable {
33         // Exceptions
34         const EXCEPTION_USERNAME_NOT_FOUND   = 0x170;
35         const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x171;
36         const EXCEPTION_USER_PASS_MISMATCH   = 0x172;
37         const EXCEPTION_USER_NOT_GUEST       = 0x173;
38
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 a user by a given request instance
51          *
52          * @param       $requestInstance        An instance of a Requestable class
53          * @return      $userInstance           An instance of this user class
54          * @todo        Add more ways over creating user classes
55          */
56         public static final function createGuestByRequest (Requestable $requestInstance) {
57                 // Determine if by email or username
58                 if (!is_null($requestInstance->getRequestElement('username'))) {
59                         // Username supplied
60                         $userInstance = self::createGuestByUserName($requestInstance->getRequestElement('username'));
61                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
62                         // Email supplied
63                         $userInstance = self::createGuestByEmail($requestInstance->getRequestElement('email'));
64                 } else {
65                         // Unsupported mode
66                         $userInstance = new Guest();
67                         $userInstance->debugBackTrace('More ways of creating user classes are needed here.');
68                         exit();
69                 }
70
71                 // Return the prepared instance
72                 return $userInstance;
73         }
74
75         /**
76          * Creates an instance of this user class by a provided username. This
77          * factory method will check if username is already taken and if not so it
78          * will throw an exception.
79          *
80          * @param       $userName               Username we need a class instance for
81          * @return      $userInstance   An instance of this user class
82          * @throws      UsernameMissingException        If the username does not exist
83          * @throws      UserNoGuestException            If the user is no guest account
84          */
85         public static final function createGuestByUsername ($userName) {
86                 // Get a new instance
87                 $userInstance = new Guest();
88
89                 // Set the username
90                 $userInstance->setUserName($userName);
91
92                 // Check if username exists
93                 if ($userInstance->ifUsernameExists() === FALSE) {
94                         // Throw an exception here
95                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
96                 } elseif ($userInstance->isGuest() === FALSE) {
97                         // Sanity check on 'guest' status failed
98                         throw new UserNoGuestException(array($userInstance, $userName), self::EXCEPTION_USER_NOT_GUEST_STATUS);
99                 }
100
101                 // Return the instance
102                 return $userInstance;
103         }
104
105         /**
106          * Creates an instance of this user class by a provided email address. This
107          * factory method will not check if email address is there.
108          *
109          * @param       $email                  Email address of the user
110          * @return      $userInstance   An instance of this user class
111          */
112         public static final function createGuestByEmail ($email) {
113                 // Get a new instance
114                 $userInstance = new Guest();
115
116                 // Set the username
117                 $userInstance->setEmail($email);
118
119                 // Return the 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                 // No activity will be logged for guest accounts
133         }
134
135         /**
136          * Flushs all pending updates to the database layer
137          *
138          * @return      void
139          */
140         public function flushPendingUpdates () {
141                 // No updates will be flushed to database!
142         }
143
144 }