3 namespace Org\Mxchange\CoreFramework\Filter\Discovery\Payment;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Action\PerformableAction;
7 use Org\Mxchange\CoreFramework\Configuration\NoConfigEntryException;
8 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Filter\BaseFilter;
10 use Org\Mxchange\CoreFramework\Filter\Chain\FilterChainException;
11 use Org\Mxchange\CoreFramework\Filter\Filterable;
12 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
13 use Org\Mxchange\CoreFramework\Loader\NoClassException;
14 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
15 use Org\Mxchange\CoreFramework\Request\Requestable;
16 use Org\Mxchange\CoreFramework\Response\Responseable;
19 * A filter for payment discovery. This class discovers the payment type and
20 * returns an object holding all available payment system for the requested
23 * @author Roland Haeder <webmaster@shipsimu.org>
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
26 * @license GNU GPL 3.0 or any newer version
27 * @link http://www.shipsimu.org
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 class PaymentDiscoveryFilter extends BaseFilter implements Filterable {
44 * Action name for payment discovery
46 private $actionName = '';
49 * Protected constructor
53 protected function __construct () {
54 // Call parent constructor
55 parent::__construct(__CLASS__);
59 * Creates an instance of this filter class
61 * @param $actionInstance A performable action
62 * @return $filterInstance An instance of this filter class
63 * @throws NullPointerException If the resolver is not set
65 public static final function createPaymentDiscoveryFilter (PerformableAction $actionInstance) {
67 $filterInstance = new PaymentDiscoveryFilter();
69 // Get resolver from action
70 $resolverInstance = $actionInstance->getResolverInstance();
72 // Is the resolver set?
73 if (is_null($resolverInstance)) {
74 // Throw an exception here
75 throw new NullPointerException($filterInstance, self::EXCEPTION_IS_NULL_POINTER);
78 // Get the action name from resolver
79 $actionName = $resolverInstance->getActionName();
81 // Store it away in this class
82 $filterInstance->setActionName($actionName);
84 // Return the instance
85 return $filterInstance;
89 * Protected setter for action name
91 * @param $actionName Action name to set
94 protected final function setActionName ($actionName) {
95 $this->actionName = (string) $actionName;
99 * Getter for action name
101 * @return $actionName Action name to set
103 public final function getActionName () {
104 return $this->actionName;
108 * Executes the filter with given request and response objects
110 * @param $requestInstance An instance of a class with an Requestable interface
111 * @param $responseInstance An instance of a class with an Responseable interface
114 * @throws FilterChainException If this filter fails to operate
116 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
117 // Try to get real discovery class
119 // Get an instance from the object factory
120 $discoveryInstance = ObjectFactory::createObjectByConfiguredName($this->getActionName() . '_payment_discovery', array($this));
122 // Call the discovery method
123 $discoveryInstance->discover($requestInstance);
125 // Remember this instance if all wents fine
126 GenericRegistry::getRegistry()->addInstance('payments', $discoveryInstance);
127 } catch (NoConfigEntryException $e) {
128 // Something bad happend
129 $requestInstance->requestIsValid(false);
131 // Add a message to the response
132 $responseInstance->addFatalMessage('payment_config_entry_error');
133 $responseInstance->addFatalMessagePlain($e->getMessage());
136 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
137 } catch (NoClassException $e) {
138 // Something bad happend
139 $requestInstance->requestIsValid(false);
141 // Add a message to the response
142 $responseInstance->addFatalMessage('payment_class_error');
143 $responseInstance->addFatalMessagePlain($e->getMessage());
146 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);