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