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