Continued:
[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 use CoreFramework\Request\Requestable;
10 use CoreFramework\Response\Responseable;
11
12 /**
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
15  * type.
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
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
22  *
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.
27  *
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.
32  *
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/>.
35  */
36 class PaymentDiscoveryFilter extends BaseFilter implements Filterable {
37         /**
38          * Action name for payment discovery
39          */
40         private $actionName = '';
41
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         protected function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50         }
51
52         /**
53          * Creates an instance of this filter class
54          *
55          * @param       $actionInstance         A performable action
56          * @return      $filterInstance         An instance of this filter class
57          * @throws      NullPointerException    If the resolver is not set
58          */
59         public static final function createPaymentDiscoveryFilter (PerformableAction $actionInstance) {
60                 // Get a new instance
61                 $filterInstance = new PaymentDiscoveryFilter();
62
63                 // Get resolver from action
64                 $resolverInstance = $actionInstance->getResolverInstance();
65
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);
70                 } // END - if
71
72                 // Get the action name from resolver
73                 $actionName = $resolverInstance->getActionName();
74
75                 // Store it away in this class
76                 $filterInstance->setActionName($actionName);
77
78                 // Return the instance
79                 return $filterInstance;
80         }
81
82         /**
83          * Protected setter for action name
84          *
85          * @param       $actionName             Action name to set
86          * @return      void
87          */
88         protected final function setActionName ($actionName) {
89                 $this->actionName = (string) $actionName;
90         }
91
92         /**
93          * Getter for action name
94          *
95          * @return      $actionName             Action name to set
96          */
97         public final function getActionName () {
98                 return $this->actionName;
99         }
100
101         /**
102          * Executes the filter with given request and response objects
103          *
104          * @param       $requestInstance        An instance of a class with an Requestable interface
105          * @param       $responseInstance       An instance of a class with an Responseable interface
106          * @return      void
107          * @todo        0% done
108          * @throws      FilterChainException    If this filter fails to operate
109          */
110         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
111                 // Try to get real discovery class
112                 try {
113                         // Get an instance from the object factory
114                         $discoveryInstance = ObjectFactory::createObjectByConfiguredName($this->getActionName() . '_payment_discovery', array($this));
115
116                         // Call the discovery method
117                         $discoveryInstance->discover($requestInstance);
118
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);
124
125                         // Add a message to the response
126                         $responseInstance->addFatalMessage('payment_config_entry_error');
127                         $responseInstance->addFatalMessagePlain($e->getMessage());
128
129                         // Abort here
130                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
131                 } catch (NoClassException $e) {
132                         // Something bad happend
133                         $requestInstance->requestIsValid(FALSE);
134
135                         // Add a message to the response
136                         $responseInstance->addFatalMessage('payment_class_error');
137                         $responseInstance->addFatalMessagePlain($e->getMessage());
138
139                         // Abort here
140                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
141                 }
142         }
143
144 }