buildServicePaymentGenericpayment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 * This source file is subject to the GNU General Public License (GPL 3)
8
 * that is bundled with this package in the file LICENSE.txt
9
 *
10
 * DISCLAIMER
11
 *
12
 * Do not edit or add to this file if you wish to upgrade Payone to newer
13
 * versions in the future. If you wish to customize Payone for your
14
 * needs please refer to http://www.payone.de for more information.
15
 *
16
 * @category        Payone
17
 * @package         Payone
18
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
19
 * @author          Matthias Walter <[email protected]>
20
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
21
 * @link            http://www.noovias.com
22
 */
23
24
/**
25
 *
26
 * @category        Payone
27
 * @package         Payone
28
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
29
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
30
 * @link            http://www.noovias.com
31
 */
32
class Payone_Builder
33
{
34
35
    const KEY_API = 'api';
36
    const KEY_CLIENTAPI = 'client_api';
37
    const KEY_PROTOCOL = 'protocol';
38
    const KEY_SETTINGS = 'settings';
39
    const KEY_TRANSACTIONSTATUS = 'transaction_status';
40
    const KEY_SESSIONSTATUS = 'session_status';
41
42
    /** @var array */
43
    protected $factories = array();
44
45
    /** @var Payone_Config */
46
    protected $config = null;
47
48
    /**
49
     * @constructor
50
     * @param null|Payone_Config $config config can be set via constructor or setConfig()
51
     */
52
    public function __construct(Payone_Config $config = null) 
53
    {
54
        if ($config === null) {
55
            $config = new Payone_Config(); // Default config
56
        }
57
58
        $this->config = $config;
59
60
        $this->factories[self::KEY_API] = new Payone_Api_Factory($config->getApiConfig());
61
        $this->factories[self::KEY_CLIENTAPI] = new Payone_ClientApi_Factory();
62
        $this->factories[self::KEY_PROTOCOL] = new Payone_Protocol_Factory();
63
        $this->factories[self::KEY_SETTINGS] = new Payone_Settings_Factory();
64
        $this->factories[self::KEY_TRANSACTIONSTATUS] = new Payone_TransactionStatus_Factory($config->getTransactionStatusConfig());
65
        $this->factories[self::KEY_SESSIONSTATUS] = new Payone_SessionStatus_Factory($config->getSessionStatusConfig());
66
    }
67
68
    /**
69
     * @api
70
     *
71
     * @return Payone_ClientApi_Service_GenerateHash
72
     */
73
    public function buildServiceClientApiGenerateHash() 
74
    {
75
        return $this->buildService(self::KEY_CLIENTAPI . '/generateHash');
76
    }
77
78
    /**
79
     * @api
80
     *
81
     * @return Payone_Api_Service_Payment_Authorize
82
     */
83
    public function buildServicePaymentAuthorize() 
84
    {
85
        return $this->buildService(self::KEY_API . '/payment/authorize');
86
    }
87
88
    /**
89
     * @api
90
     *
91
     * @return Payone_Api_Service_Payment_Preauthorize
92
     */
93
    public function buildServicePaymentPreauthorize() 
94
    {
95
        return $this->buildService(self::KEY_API . '/payment/preauthorize');
96
    }
97
98
    /**
99
     * @api
100
     *
101
     * @return Payone_Api_Service_Payment_Capture
102
     */
103
    public function buildServicePaymentCapture() 
104
    {
105
        return $this->buildService(self::KEY_API . '/payment/capture');
106
    }
107
108
    /**
109
     * @api
110
     *
111
     * @return Payone_Api_Service_Payment_Debit
112
     */
113
    public function buildServicePaymentDebit() 
114
    {
115
        return $this->buildService(self::KEY_API . '/payment/debit');
116
    }
117
118
    /**
119
     * @api
120
     *
121
     * @return Payone_Api_Service_Payment_Refund
122
     */
123
    public function buildServicePaymentRefund() 
124
    {
125
        return $this->buildService(self::KEY_API . '/payment/refund');
126
    }
127
128
    /**
129
     * @api
130
     *
131
     * @return Payone_Api_Service_Verification_3dsCheck
132
     */
133
    public function buildServiceVerification3dsCheck() 
134
    {
135
        return $this->buildService(self::KEY_API . '/verification/3dscheck');
136
    }
137
138
    /**
139
     * @api
140
     *
141
     * @return Payone_Api_Service_Management_GetInvoice
142
     */
143
    public function buildServiceManagementGetInvoice() 
144
    {
145
        return $this->buildService(self::KEY_API . '/management/getInvoice');
146
    }
147
148
    /**
149
     * @api
150
     *
151
     * @return Payone_Api_Service_Management_GetFile
152
     */
153
    public function buildServiceManagementGetFile() 
154
    {
155
        return $this->buildService(self::KEY_API . '/management/getFile');
156
    }
157
158
    /**
159
     * @api
160
     *
161
     * @return Payone_Api_Service_Management_ManageMandate
162
     */
163
    public function buildServiceManagementManageMandate() 
164
    {
165
        return $this->buildService(self::KEY_API . '/management/manageMandate');
166
    }
167
168
    /**
169
     * @api
170
     *
171
     * @return Payone_Api_Service_Verification_AddressCheck
172
     */
173
    public function buildServiceVerificationAddressCheck() 
174
    {
175
        return $this->buildService(self::KEY_API . '/verification/addressCheck');
176
    }
177
178
    /**
179
     * @api
180
     *
181
     * @return Payone_Api_Service_Verification_CreditCardCheck
182
     */
183
    public function buildServiceVerificationCreditCardCheck() 
184
    {
185
        return $this->buildService(self::KEY_API . '/verification/creditCardCheck');
186
    }
187
188
    /**
189
     * @api
190
     *
191
     * @return Payone_Api_Service_Verification_BankAccountCheck
192
     */
193
    public function buildServiceVerificationBankAccountCheck() 
194
    {
195
        return $this->buildService(self::KEY_API . '/verification/bankAccountCheck');
196
    }
197
198
    /**
199
     * @api
200
     *
201
     * @return Payone_Api_Service_Verification_Consumerscore
202
     */
203
    public function buildServiceVerificationConsumerscore() 
204
    {
205
        return $this->buildService(self::KEY_API . '/verification/consumerscore');
206
    }
207
208
    /**
209
     * @api
210
     *
211
     * @return Payone_Settings_Service_XmlGenerate
212
     */
213
    public function buildServiceSettingsXmlGenerate() 
214
    {
215
        return $this->buildService(self::KEY_SETTINGS . '/xmlgenerate');
216
    }
217
218
    /**
219
     * Service to start the paypal express checkout and
220
     * in step two get customers shipping address from paypal.
221
     * usage:
222
     * $builder = $this->getPayoneBuilder();
223
     * $service = $builder->buildServicePaymentGenericpayment();
224
     * $response = $service->request($request);
225
     * 
226
     * @api
227
     * @return Payone_Api_Service_Payment_Genericpayment
228
     */
229
    public function buildServicePaymentGenericpayment() 
230
    {
231
        return $this->buildService(self::KEY_API . '/payment/genericpayment');
232
    }
233
234
    /**
235
     * @api
236
     * @param $key
237
     * @param array $validIps
238
     * @return Payone_TransactionStatus_Service_HandleRequest
239
     */
240 View Code Duplication
    public function buildServiceTransactionStatusHandleRequest($key, array $validIps) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
    {
242
        /** @var $service Payone_TransactionStatus_Service_HandleRequest */
243
        $service = $this->buildService(self::KEY_TRANSACTIONSTATUS . '/handlerequest');
244
        $validators = $service->getValidators();
245
246
        foreach ($validators as $validator) {
247
            if ($validator instanceof Payone_TransactionStatus_Validator_DefaultParameters) {
248
                /** @var $validator Payone_TransactionStatus_Validator_DefaultParameters */
249
                $validator->setKey($key);
250
            } elseif ($validator instanceof Payone_TransactionStatus_Validator_Ip) {
251
                /** @var $validator Payone_TransactionStatus_Validator_Ip */
252
                $validator->setValidIps($validIps);
253
                $validator->setConfig($this->getConfig()->getTransactionStatusConfig());
254
            }
255
        }
256
257
        return $service;
258
    }
259
260
    /**
261
     * @api
262
     * @param $key
263
     * @param array $validIps
264
     * @return Payone_SessionStatus_Service_HandleRequest
265
     */
266 View Code Duplication
    public function buildServiceSessionStatusHandleRequest($key, array $validIps) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
267
    {
268
        /** @var $service Payone_SessionStatus_Service_HandleRequest */
269
        $service = $this->buildService(self::KEY_SESSIONSTATUS . '/handlerequest');
270
        $validators = $service->getValidators();
271
272
        foreach ($validators as $validator) {
273
            if ($validator instanceof Payone_SessionStatus_Validator_DefaultParameters) {
274
                /** @var $validator Payone_SessionStatus_Validator_DefaultParameters */
275
                $validator->setKey($key);
276
            } elseif ($validator instanceof Payone_SessionStatus_Validator_Ip) {
277
                /** @var $validator Payone_SessionStatus_Validator_Ip */
278
                $validator->setValidIps($validIps);
279
                $validator->setConfig($this->getConfig()->getSessionStatusConfig());
280
            }
281
        }
282
283
        return $service;
284
    }
285
286
    /**
287
     * @param string $key Service key, e.g. "api/payment/authorize"
288
     * @return Payone_Api_Service_Payment_Abstract
289
     * @throws Exception
290
     */
291
    protected function buildService($key) 
292
    {
293
        $config = $this->getConfig();
294
295
        $keyArray = explode('/', $key);
296
        $factoryKey = array_shift($keyArray);
297
        $serviceKey = implode('/', $keyArray);
298
299
        // Load required factory:
300
        $factory = $this->getFactory($factoryKey);
301
302
        $service = $factory->buildService($serviceKey);
0 ignored issues
show
Bug introduced by
The method buildService does only exist in Payone_Api_Factory and P...ansactionStatus_Factory, but not in Payone_Protocol_Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
303
304
        // Add Protocol Service, if required, with custom or default config:
305 View Code Duplication
        if (method_exists($service, 'setServiceProtocol')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
306
            $protocolConfig = $config->getValue($factoryKey . '/' . $serviceKey . '/protocol');
307
            if ($protocolConfig == null) {
308
                $protocolConfig = $config->getValue($factoryKey . '/default/protocol');
309
            }
310
311
            $serviceProtocol = $this->buildServiceProtocol($protocolConfig, $factoryKey);
312
            $service->setServiceProtocol($serviceProtocol);
313
        }
314
315 View Code Duplication
        if (method_exists($service, 'setValidator')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
316
            $validatorConfig = $config->getValue($factoryKey . '/' . $serviceKey . '/validator');
317
            if ($validatorConfig == null) {
318
                $validatorConfig = $config->getValue($factoryKey . '/default/validator');
319
            }
320
321
            $validator = $this->buildServiceValidation($validatorConfig);
322
            if ($validator !== null) {
323
                $service->setValidator($validator);
324
            }
325
        }
326
327 View Code Duplication
        if (method_exists($service, 'setValidators')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
328
            $validatorConfig = $config->getValue($factoryKey . '/' . $serviceKey . '/validators');
329
            if ($validatorConfig == null) {
330
                $validatorConfig = $config->getValue($factoryKey . '/default/validators');
331
            }
332
333
            $validator = $this->buildServiceValidation($validatorConfig);
334
            if ($validator !== null) {
335
                $service->setValidators($validator);
336
            }
337
        }
338
339
        return $service;
340
    }
341
342
    /**
343
     * @param array $protocolConfig
344
     * @param string $factoryKey
345
     * @return Payone_Protocol_Service_Protocol_Abstract
346
     */
347
    protected function buildServiceProtocol(array $protocolConfig, $factoryKey) 
348
    {
349
        $serviceProtocol = $this->getFactory($factoryKey)->buildServiceProtocolRequest();
0 ignored issues
show
Bug introduced by
The method buildServiceProtocolRequest does only exist in Payone_Api_Factory and P...ansactionStatus_Factory, but not in Payone_Protocol_Factory ...Payone_Settings_Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
350
        $serviceApplyFilters = $this->getFactory(self::KEY_PROTOCOL)->buildServiceApplyFilters();
0 ignored issues
show
Bug introduced by
The method buildServiceApplyFilters does only exist in Payone_Protocol_Factory, but not in Payone_Api_Factory and P...ansactionStatus_Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
351
352
        // Load filters by config:
353
        if (array_key_exists('filter', $protocolConfig)) {
354
            foreach ($protocolConfig['filter'] as $key => $options) {
355
                if ($options['enabled'] === TRUE || $options['enabled'] === 1) {
356
                    // @todo hs: un-elegant, kann man das hier ohne switch machen?
357
                    switch ($key) {
358
                        case Payone_Protocol_Filter_MaskValue::FILTER_KEY :
359
                            $filterMaskValue = new Payone_Protocol_Filter_MaskValue();
360
                            $filterMaskValue->setConfig('percent', $options['percent']);
361
                            $serviceApplyFilters->addFilter($filterMaskValue);
362
                            break;
363
                        case Payone_Protocol_Filter_MaskAllValue::FILTER_KEY :
364
                            $filterMaskAllValue = new Payone_Protocol_Filter_MaskAllValue();
365
                            $serviceApplyFilters->addFilter($filterMaskAllValue);
366
                            break;
367
                    }
368
                }
369
            }
370
        }
371
372
        $serviceProtocol->setServiceApplyFilters($serviceApplyFilters);
373
374 View Code Duplication
        if (array_key_exists('loggers', $protocolConfig)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
375
            $loggerConfig = $protocolConfig['loggers'];
376
            if (is_array($loggerConfig) and count($loggerConfig) > 0) {
377
                foreach ($loggerConfig as $className => $options) {
378
                    if (class_exists($className)) {
379
                        /** @var $logger Payone_Protocol_Logger_Interface * */
380
                        $logger = new $className;
381
                        if (method_exists($logger, 'setConfig')) {
382
                            $logger->setConfig($options);
383
                        }
384
385
                        $serviceProtocol->addLogger($logger);
386
                    }
387
                }
388
            }
389
        }
390
391
        // @todo hs: repository section, for now as a separate array, could this be combined with the loggers?
392 View Code Duplication
        if (array_key_exists('repositories', $protocolConfig)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
393
            $respositoryConfig = $protocolConfig['repositories'];
394
            if (is_array($respositoryConfig) and count($respositoryConfig) > 0) {
395
                foreach ($respositoryConfig as $className => $options) {
396
                    if (class_exists($className)) {
397
                        // @todo hs: what do we do with Payone_Api_Persistence_Interface?
398
                        /** @var $repository Payone_TransactionStatus_Persistence_Interface * */
399
                        $repository = new $className;
400
                        if (method_exists($repository, 'setConfig')) {
401
                            $repository->setConfig($options);
0 ignored issues
show
Bug introduced by
The method setConfig() does not seem to exist on object<Payone_Transactio..._Persistence_Interface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
402
                        }
403
404
                        $serviceProtocol->addRepository($repository);
405
                    }
406
                }
407
            }
408
        }
409
410
        return $serviceProtocol;
411
    }
412
413
    /**
414
     * @param $validatorConfig
415
     * @return null|validator
416
     */
417
    protected function buildServiceValidation($validatorConfig) 
418
    {
419
        if (is_array($validatorConfig)) {
420
            $validator = array();
421
            foreach ($validatorConfig as $config) {
422
                if ($config === 'default' or ! class_exists($config)) {
423
                    return null;
424
                } else {
425
                    $validator[] = new $config();
426
                }
427
            }
428
429
            return $validator;
430
        } else {
431
            // Load validator by config (if non-default):
432
            if ($validatorConfig === 'default' or ! class_exists($validatorConfig)) {
433
                return null;
434
            } else {
435
                $validator = new $validatorConfig();
436
                return $validator;
437
            }
438
        }
439
    }
440
441
    /**
442
     * @param \Payone_Config $config
443
     */
444
    public function setConfig(Payone_Config $config) 
445
    {
446
        $this->config = $config;
447
    }
448
449
    /**
450
     * @return \Payone_Config
451
     */
452
    protected function getConfig() 
453
    {
454
        return $this->config;
455
    }
456
457
    /**
458
     * @param $key
459
     * @return null|Payone_Api_Factory|Payone_Protocol_Factory|Payone_Settings_Factory|Payone_TransactionStatus_Factory
460
     * @throws Exception
461
     */
462
    protected function getFactory($key) 
463
    {
464
        if (array_key_exists($key, $this->factories)) {
465
            return $this->factories[$key];
466
        } else {
467
            throw new Exception('Could not get internal factory with key "' . $key . '"');
468
        }
469
    }
470
471
}
472