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