3 namespace Org\Mxchange\CoreFramework\Filter\Discovery\Payment;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Action\PerformableAction;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Filter\BaseFilter;
9 use Org\Mxchange\CoreFramework\Filter\Filterable;
10 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
11 use Org\Mxchange\CoreFramework\Loader\NoClassException;
12 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
13 use Org\Mxchange\CoreFramework\Request\Requestable;
14 use Org\Mxchange\CoreFramework\Response\Responseable;
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
21 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
40 class PaymentDiscoveryFilter extends BaseFilter implements Filterable {
42 * Action name for payment discovery
44 private $actionName = '';
47 * Protected constructor
51 protected function __construct () {
52 // Call parent constructor
53 parent::__construct(__CLASS__);
57 * Creates an instance of this filter class
59 * @param $actionInstance A performable action
60 * @return $filterInstance An instance of this filter class
61 * @throws NullPointerException If the resolver is not set
63 public static final function createPaymentDiscoveryFilter (PerformableAction $actionInstance) {
65 $filterInstance = new PaymentDiscoveryFilter();
67 // Get resolver from action
68 $resolverInstance = $actionInstance->getResolverInstance();
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);
76 // Get the action name from resolver
77 $actionName = $resolverInstance->getActionName();
79 // Store it away in this class
80 $filterInstance->setActionName($actionName);
82 // Return the instance
83 return $filterInstance;
87 * Protected setter for action name
89 * @param $actionName Action name to set
92 protected final function setActionName ($actionName) {
93 $this->actionName = (string) $actionName;
97 * Getter for action name
99 * @return $actionName Action name to set
101 public final function getActionName () {
102 return $this->actionName;
106 * Executes the filter with given request and response objects
108 * @param $requestInstance An instance of a class with an Requestable interface
109 * @param $responseInstance An instance of a class with an Responseable interface
112 * @throws FilterChainException If this filter fails to operate
114 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
115 // Try to get real discovery class
117 // Get an instance from the object factory
118 $discoveryInstance = ObjectFactory::createObjectByConfiguredName($this->getActionName() . '_payment_discovery', array($this));
120 // Call the discovery method
121 $discoveryInstance->discover($requestInstance);
123 // Remember this instance if all wents fine
124 GenericRegistry::getRegistry()->addInstance('payments', $discoveryInstance);
125 } catch (NoConfigEntryException $e) {
126 // Something bad happend
127 $requestInstance->requestIsValid(false);
129 // Add a message to the response
130 $responseInstance->addFatalMessage('payment_config_entry_error');
131 $responseInstance->addFatalMessagePlain($e->getMessage());
134 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
135 } catch (NoClassException $e) {
136 // Something bad happend
137 $requestInstance->requestIsValid(false);
139 // Add a message to the response
140 $responseInstance->addFatalMessage('payment_class_error');
141 $responseInstance->addFatalMessagePlain($e->getMessage());
144 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);