Renamed Registry -> GenericRegistry to make it clear that this registry does
[core.git] / framework / main / classes / filter / validator / class_UserNameValidatorFilter.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filter\Validator\Username;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filter\BaseFilter;
7 use Org\Mxchange\CoreFramework\Filter\Filterable;
8 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
9 use Org\Mxchange\CoreFramework\Request\Requestable;
10 use Org\Mxchange\CoreFramework\Response\Responseable;
11
12 /**
13  * A concrete filter for validating the username. This filter may intercept the
14  * filter chain if no username is given or if supplied username has an invalid
15  * form. It could also intercept our filter chain if username is already taken.
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35  */
36 class UserNameValidatorFilter extends BaseFilter implements Filterable {
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 an instance of this filter class
49          *
50          * @return      $filterInstance                 An instance of this filter class
51          */
52         public static final function createUserNameValidatorFilter () {
53                 // Get a new instance
54                 $filterInstance = new UserNameValidatorFilter();
55
56                 // Return the instance
57                 return $filterInstance;
58         }
59
60         /**
61          * Executes the filter with given request and response objects
62          *
63          * @param       $requestInstance        An instance of a class with an Requestable interface
64          * @param       $responseInstance       An instance of a class with an Responseable interface
65          * @return      void
66          * @throws      FilterChainException    If this filter fails to operate
67          */
68         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
69                 // Get username from request
70                 $userName = $requestInstance->getRequestElement('username');
71
72                 // Is the username set?
73                 if (is_null($userName)) {
74                         // Not found in form so stop the filtering process
75                         $requestInstance->requestIsValid(false);
76
77                         // Add a message to the response
78                         $responseInstance->addFatalMessage('username_unset');
79
80                         // Abort here
81                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
82                 } elseif (empty($userName)) {
83                         // Empty field!
84                         $requestInstance->requestIsValid(false);
85
86                         // Add a message to the response
87                         $responseInstance->addFatalMessage('username_empty');
88
89                         // Abort here
90                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
91                 } elseif ($this->ifUserNameIsTaken($userName)) {
92                         // Username is already taken
93                         $requestInstance->requestIsValid(false);
94
95                         // Add a message to the response
96                         $responseInstance->addFatalMessage('username_taken');
97
98                         // Abort here
99                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
100                 }
101         }
102
103         /**
104          * Check whether the username as already been taken
105          *
106          * @param       $userName               Username to check for existence
107          * @return      $alreadyTaken   Whether the username has been taken
108          */
109         private function ifUserNameIsTaken ($userName) {
110                 // Default is already taken
111                 $alreadyTaken = true;
112
113                 // Initialize instance
114                 $userInstance = NULL;
115
116                 // Get a registry instance
117                 $registry = GenericRegistry::getRegistry();
118
119                 // Is the user already there?
120                 if ($registry->instanceExists('user')) {
121                         // Use the instance for checking for the email
122                         $userInstance = $registry->getInstance('user');
123                         $userInstance->setUserName($userName);
124                 } else {
125                         // If this instance is created then the username *does* exist
126                         try {
127                                 // Get a new instance
128                                 $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('user_class'), 'createMemberByUsername'), array($userName));
129
130                                 // Remember this user instance in our registry for later usage
131                                 $registry->addInstance('user', $userInstance);
132                         } catch (UsernameMissingException $e) {
133                                 // User was not found
134                         }
135                 }
136
137                 // Does the username exist?
138                 if ((is_null($userInstance)) || ($userInstance->ifUsernameExists() === false)) {
139                         // This username is still available
140                         $alreadyTaken = false;
141                 } // END - if
142
143                 // Return the result
144                 return $alreadyTaken;
145         }
146
147 }