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