3 namespace CoreFramework\Filter\Discovery\Payment;
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 use CoreFramework\Response\Responseable;
13 * A filter for payment discovery. This class discovers the payment type and
14 * returns an object holding all available payment system for the requested
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class PaymentDiscoveryFilter extends BaseFilter implements Filterable {
38 * Action name for payment discovery
40 private $actionName = '';
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
53 * Creates an instance of this filter class
55 * @param $actionInstance A performable action
56 * @return $filterInstance An instance of this filter class
57 * @throws NullPointerException If the resolver is not set
59 public static final function createPaymentDiscoveryFilter (PerformableAction $actionInstance) {
61 $filterInstance = new PaymentDiscoveryFilter();
63 // Get resolver from action
64 $resolverInstance = $actionInstance->getResolverInstance();
66 // Is the resolver set?
67 if (is_null($resolverInstance)) {
68 // Throw an exception here
69 throw new NullPointerException($filterInstance, self::EXCEPTION_IS_NULL_POINTER);
72 // Get the action name from resolver
73 $actionName = $resolverInstance->getActionName();
75 // Store it away in this class
76 $filterInstance->setActionName($actionName);
78 // Return the instance
79 return $filterInstance;
83 * Protected setter for action name
85 * @param $actionName Action name to set
88 protected final function setActionName ($actionName) {
89 $this->actionName = (string) $actionName;
93 * Getter for action name
95 * @return $actionName Action name to set
97 public final function getActionName () {
98 return $this->actionName;
102 * Executes the filter with given request and response objects
104 * @param $requestInstance An instance of a class with an Requestable interface
105 * @param $responseInstance An instance of a class with an Responseable interface
108 * @throws FilterChainException If this filter fails to operate
110 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
111 // Try to get real discovery class
113 // Get an instance from the object factory
114 $discoveryInstance = ObjectFactory::createObjectByConfiguredName($this->getActionName() . '_payment_discovery', array($this));
116 // Call the discovery method
117 $discoveryInstance->discover($requestInstance);
119 // Remember this instance if all wents fine
120 Registry::getRegistry()->addInstance('payments', $discoveryInstance);
121 } catch (NoConfigEntryException $e) {
122 // Something bad happend
123 $requestInstance->requestIsValid(FALSE);
125 // Add a message to the response
126 $responseInstance->addFatalMessage('payment_config_entry_error');
127 $responseInstance->addFatalMessagePlain($e->getMessage());
130 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
131 } catch (NoClassException $e) {
132 // Something bad happend
133 $requestInstance->requestIsValid(FALSE);
135 // Add a message to the response
136 $responseInstance->addFatalMessage('payment_class_error');
137 $responseInstance->addFatalMessagePlain($e->getMessage());
140 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);