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