Continued:
[core.git] / framework / main / classes / filter / change / class_EmailChangeFilter.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filter\Change\Email;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filter\BaseFilter;
7 use Org\Mxchange\CoreFramework\Filter\Chain\FilterChainException;
8 use Org\Mxchange\CoreFramework\Filter\Filterable;
9 use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
10 use Org\Mxchange\CoreFramework\Registry\Object\ObjectRegistry;
11 use Org\Mxchange\CoreFramework\Request\Requestable;
12 use Org\Mxchange\CoreFramework\Response\Responseable;
13
14 /**
15  * A filter for detecting email changes
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 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 EmailChangeFilter extends BaseFilter implements Filterable {
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         private 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 createEmailChangeFilter () {
53                 // Get a new instance
54                 $filterInstance = new EmailChangeFilter();
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          * @todo        Implement email change of the user here. HINT: Use the User class!
68          */
69         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
70                 // Get both emails
71                 $email1 = $requestInstance->getRequestElement('email1');
72                 $email2 = $requestInstance->getRequestElement('email2');
73
74                 // Is only first email set?
75                 if ((!empty($email1)) && (empty($email2))) {
76                         // Request is invalid!
77                         $requestInstance->setIsRequestValid(FALSE);
78
79                         // Email 2 is empty
80                         $responseInstance->addFatalMessage('email2_empty');
81
82                         // Stop processing here
83                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
84                 }
85
86                 // Is only second email set?
87                 if ((empty($email1)) && (!empty($email2))) {
88                         // Request is invalid!
89                         $requestInstance->setIsRequestValid(FALSE);
90
91                         // Email 1 is empty
92                         $responseInstance->addFatalMessage('email1_empty');
93
94                         // Stop processing here
95                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
96                 }
97
98                 // Do both match?
99                 if ($email1 != $email2) {
100                         // Request is invalid!
101                         $requestInstance->setIsRequestValid(FALSE);
102
103                         // Emails are mismatching
104                         $responseInstance->addFatalMessage('emails_mismatch');
105
106                         // Stop processing here
107                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
108                 }
109
110                 // Are email and confirmation empty?
111                 if ((empty($email1)) && (empty($email2))) {
112                         // No email change required!
113                         return true;
114                 }
115
116                 // Now, get a user instance for comparison
117                 $userInstance = ObjectRegistry::getRegistry('generic')->getInstance('user');
118
119                 // Get the email field
120                 $userEmail = $userInstance->getField('email');
121
122                 // Are they different?
123                 if ($userEmail == $email1) {
124                         // Nothing has been changed is fine...
125                         return true;
126                 }
127
128                 // Update the "new_email" field
129                 DebugMiddleware::getSelfInstance()->partialStub('Unfinished part.');
130         }
131
132 }