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