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