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