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