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