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