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