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