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