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