]> git.mxchange.org Git - core.git/blob - framework/main/classes/filter/payment/class_PaymentDiscoveryFilter.php
Continued:
[core.git] / framework / main / classes / filter / payment / class_PaymentDiscoveryFilter.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filter\Discovery\Payment;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Action\PerformableAction;
7 use Org\Mxchange\CoreFramework\Configuration\NoConfigEntryException;
8 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Filter\BaseFilter;
10 use Org\Mxchange\CoreFramework\Filter\Chain\FilterChainException;
11 use Org\Mxchange\CoreFramework\Filter\Filterable;
12 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
13 use Org\Mxchange\CoreFramework\Loader\NoClassException;
14 use Org\Mxchange\CoreFramework\Registry\Object\ObjectRegistry;
15 use Org\Mxchange\CoreFramework\Request\Requestable;
16 use Org\Mxchange\CoreFramework\Response\Responseable;
17
18 /**
19  * A filter for payment discovery. This class discovers the payment type and
20  * returns an object holding all available payment system for the requested
21  * type.
22  *
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             0.0.0
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
26  * @license             GNU GPL 3.0 or any newer version
27  * @link                http://www.shipsimu.org
28  *
29  * This program is free software: you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation, either version 3 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program. If not, see <http://www.gnu.org/licenses/>.
41  */
42 class PaymentDiscoveryFilter extends BaseFilter implements Filterable {
43         /**
44          * Action name for payment discovery
45          */
46         private $actionName = '';
47
48         /**
49          * Protected constructor
50          *
51          * @return      void
52          */
53         private function __construct () {
54                 // Call parent constructor
55                 parent::__construct(__CLASS__);
56         }
57
58         /**
59          * Creates an instance of this filter class
60          *
61          * @param       $actionInstance         A performable action
62          * @return      $filterInstance         An instance of this filter class
63          * @throws      NullPointerException    If the resolver is not set
64          */
65         public static final function createPaymentDiscoveryFilter (PerformableAction $actionInstance) {
66                 // Get a new instance
67                 $filterInstance = new PaymentDiscoveryFilter();
68
69                 // Get resolver from action
70                 $resolverInstance = $actionInstance->getResolverInstance();
71
72                 // Is the resolver set?
73                 if (is_null($resolverInstance)) {
74                         // Throw an exception here
75                         throw new NullPointerException($filterInstance, FrameworkInterface::EXCEPTION_IS_NULL_POINTER);
76                 }
77
78                 // Get the action name from resolver
79                 $actionName = $resolverInstance->getActionName();
80
81                 // Store it away in this class
82                 $filterInstance->setActionName($actionName);
83
84                 // Return the instance
85                 return $filterInstance;
86         }
87
88         /**
89          * Protected setter for action name
90          *
91          * @param       $actionName             Action name to set
92          * @return      void
93          */
94         protected final function setActionName (string $actionName) {
95                 $this->actionName = $actionName;
96         }
97
98         /**
99          * Getter for action name
100          *
101          * @return      $actionName             Action name to set
102          */
103         public final function getActionName () {
104                 return $this->actionName;
105         }
106
107         /**
108          * Executes the filter with given request and response objects
109          *
110          * @param       $requestInstance        An instance of a class with an Requestable interface
111          * @param       $responseInstance       An instance of a class with an Responseable interface
112          * @return      void
113          * @todo        0% done
114          * @throws      FilterChainException    If this filter fails to operate
115          */
116         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
117                 // Try to get real discovery class
118                 try {
119                         // Get an instance from the object factory
120                         $discoveryInstance = ObjectFactory::createObjectByConfiguredName($this->getActionName() . '_payment_discovery', [$this]);
121
122                         // Call the discovery method
123                         $discoveryInstance->discover($requestInstance);
124
125                         // Remember this instance if all wents fine
126                         ObjectRegistry::getRegistry('generic')->addInstance('payments', $discoveryInstance);
127                 } catch (NoConfigEntryException $e) {
128                         // Something bad happend
129                         $requestInstance->setIsRequestValid(FALSE);
130
131                         // Add a message to the response
132                         $responseInstance->addFatalMessage('payment_config_entry_error');
133                         $responseInstance->addFatalMessagePlain($e->getMessage());
134
135                         // Abort here
136                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
137                 } catch (NoClassException $e) {
138                         // Something bad happend
139                         $requestInstance->setIsRequestValid(FALSE);
140
141                         // Add a message to the response
142                         $responseInstance->addFatalMessage('payment_class_error');
143                         $responseInstance->addFatalMessagePlain($e->getMessage());
144
145                         // Abort here
146                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
147                 }
148         }
149
150 }