mapParameterInvoice()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
1
<?php
2
/**
3
 *
4
 * NOTICE OF LICENSE
5
 *
6
 * This source file is subject to the GNU General Public License (GPL 3)
7
 * that is bundled with this package in the file LICENSE.txt
8
 *
9
 * DISCLAIMER
10
 *
11
 * Do not edit or add to this file if you wish to upgrade Payone to newer
12
 * versions in the future. If you wish to customize Payone for your
13
 * needs please refer to http://www.payone.de for more information.
14
 *
15
 * @category        Payone
16
 * @package         Payone_Settings
17
 * @subpackage      Service
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_Settings
28
 * @subpackage      Service
29
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
30
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
31
 * @link            http://www.noovias.com
32
 */
33
class Payone_Settings_Service_XmlGenerate
34
{
35
    const TAG_CONFIG_ROOT = 'config';
36
    const CLASS_PREFIX = 'Payone_Settings_Data_ConfigFile_';
37
38
    /** @var DOMDocument */
39
    private $dom;
40
41
    // @todo neue Methode generate mit gleichen Parametern
42
    // @todo wandelt anhand Shop "gruppiert" um und fügt mehrere Shops umwandlungen zusammen in ein config-root
43
    // @todo innerhalb von mapShop wird dann ein mapSystem, mapGlobal, mapClearingtypes und mapProtect gerufen
44
    // @todo innerhalb dieser methoden je nach bedarf weitere Methoden
45
46
    /**
47
     * Generates an XML string from a Root config object, including all settings (global, shop, payment, protect)
48
     *
49
     * @api
50
     *
51
     * @param Payone_Settings_Data_ConfigFile_Root $config
52
     * @return mixed @see SimpleXMLElement::asXml()
53
     */
54
    public function generate(Payone_Settings_Data_ConfigFile_Root $config)
55
    {
56
        // @todo wandelt anhand Shop "gruppiert" um und fügt mehrere Shops umwandlungen zusammen in ein config-root
57
        $this->dom = new DOMDocument('1.0', 'UTF-8');
58
59
        $root = $this->appendElement($this->dom, $config->getKey());
60
61
        foreach ($config->getShop() as $key => $value) {
62
            $shop = $this->mapShop($value, $root);
0 ignored issues
show
Unused Code introduced by
$shop is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
        }
64
65
        $this->dom->formatOutput = true;
66
        $xmlString = $this->dom->saveXML();
67
68
        return $xmlString;
69
    }
70
71
    /**
72
     * Generates an XML string from a Root config object, including all settings (global, shop, payment, protect)
73
     *
74
     * @api
75
     *
76
     * @param Payone_Settings_Data_ConfigFile_Root $config
77
     * @return mixed @see SimpleXMLElement::asXml()
78
     */
79
    public function execute(Payone_Settings_Data_ConfigFile_Root $config)
80
    {
81
        // Recursively add the arrays to a SimpleXMLElement, forming a tree:
82
        $arrayData = $config->toArray();
83
        $xml = $this->simpleXmlFromNestedArray(self::TAG_CONFIG_ROOT, $arrayData);
84
85
        return $xml->asXML();
86
    }
87
88
    /**
89
     * @param string $name                tag name
90
     * @param array $array                data
91
     * @param null|SimpleXMLElement $root IF not set, $name will form the root element
92
     * @return SimpleXMLElement
93
     */
94
    public function simpleXmlFromNestedArray($name, $array, SimpleXMLElement $root = null)
95
    {
96
        if ($root === null) {
97
            $root = new SimpleXMLElement('<' . $name . '>' . '</' . $name . '>');
98
        }
99
100
        /** @var $parent SimpleXMLElement */
101
        $parent = $root->addChild($name);
102
        foreach ($array as $key => $value) {
103
            if (is_array($value)) {
104
                if (array_key_exists('attribute', $value)) {
105
                    //add node
106
                    $node = $parent->addChild($value['node']);
107
                    //add all attributes
108
                    foreach ($value['attribute'] as $attributKey => $attributData) {
109
                        $node->addAttribute($attributKey, $attributData);
110
                    }
111
                }
112
                else {
113
                    $this->simpleXmlFromNestedArray($key, $value, $parent);
114
                }
115
            }
116
            else {
117
                $parent->addChild($key, $value);
118
            }
119
        }
120
121
        return $parent;
122
    }
123
124
    /**
125
     * @param Payone_Settings_Data_ConfigFile_Shop $shopConfig
126
     * @param DOMElement $configXml
127
     * @return string
128
     */
129
    protected function mapShop(Payone_Settings_Data_ConfigFile_Shop $shopConfig, DOMElement $configXml)
130
    {
131
        $shopXml = $this->appendElement($configXml, $shopConfig->getKey());
132
133
        $this->addChild($shopXml, $shopConfig, 'code');
134
        $this->addChild($shopXml, $shopConfig, 'name', true);
135
136
        $shopXml = $this->mapSystem($shopConfig->getSystem(), $shopXml);
137
138
        $shopXml = $this->mapGlobal($shopConfig->getGlobal(), $shopXml);
139
        $shopXml = $this->mapClearingtypes($shopConfig->getClearingtypes(), $shopXml);
140
        $shopXml = $this->mapProtect($shopConfig->getProtect(), $shopXml);
141
        $shopXml = $this->mapMisc($shopConfig->getMisc(), $shopXml);
142
143
        return $shopXml;
144
    }
145
146
    /**
147
     * @param Payone_Settings_Data_ConfigFile_Shop_System $systemConfig
148
     * @param DOMElement $shopXml
149
     * @return DOMElement
150
     */
151
    protected function mapSystem(Payone_Settings_Data_ConfigFile_Shop_System $systemConfig, DOMElement $shopXml)
152
    {
153
        $systemXml = $this->appendElement($shopXml, $systemConfig->getKey());
154
155
        $this->addChild($systemXml, $systemConfig, 'name');
156
        $this->addChild($systemXml, $systemConfig, 'version');
157
        $this->addChild($systemXml, $systemConfig, 'edition');
158
        $this->addChild($systemXml, $systemConfig, 'modules');
159
        return $shopXml;
160
    }
161
162
    /**
163
     * @param Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig
164
     * @param DOMElement $shopXml
165
     * @return DOMElement
166
     */
167
    protected function mapGlobal(Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig, DOMElement $shopXml)
168
    {
169
        $globalXml = $this->appendElement($shopXml, $globalConfig->getKey());
170
171
        $this->addChild($globalXml, $globalConfig, 'mid');
172
        $this->addChild($globalXml, $globalConfig, 'aid');
173
        $this->addChild($globalXml, $globalConfig, 'portalid');
174
        $this->addChild($globalXml, $globalConfig, 'request_type');
175
        $this->mapParameterInvoice($globalConfig, $globalXml);
176
        $this->addStatusMapping($globalConfig, $globalXml);
177
        $this->mapPaymentCreditcard($globalConfig, $globalXml);
178
        return $shopXml;
179
    }
180
181
    /**
182
     * @param Payone_Settings_Data_ConfigFile_Shop_ClearingTypes $clearingTypes
183
     * @param DOMElement $shopXml
184
     * @return DOMElement
185
     */
186
    protected function mapClearingTypes(Payone_Settings_Data_ConfigFile_Shop_ClearingTypes $clearingTypes, DOMElement $shopXml)
187
    {
188
        $clearingTypesXml = $this->appendElement($shopXml, $clearingTypes->getKey());
189
190
        foreach ($clearingTypes->getClearingtypes() as $keyClearingType => $valueClearingType) {
191
            $clearingTypeNode = $this->appendElement($clearingTypesXml, $valueClearingType->getKey());
192
193
            $this->addChild($clearingTypeNode, $valueClearingType, 'title', true);
194
            $this->addChild($clearingTypeNode, $valueClearingType, 'id');
195
            $this->addChild($clearingTypeNode, $valueClearingType, 'mid');
196
            $this->addChild($clearingTypeNode, $valueClearingType, 'aid');
197
            $this->addChild($clearingTypeNode, $valueClearingType, 'portalid');
198
            $this->addFeeConfig($clearingTypeNode, $valueClearingType);
199
            $this->addChild($clearingTypeNode, $valueClearingType, 'min_order_total');
200
            $this->addChild($clearingTypeNode, $valueClearingType, 'max_order_total');
201
202
            $this->addTypesOrGlobalInfo($clearingTypeNode, $valueClearingType);
203
        }
204
205
        return $shopXml;
206
    }
207
208
    /**
209
     * @param Payone_Settings_Data_ConfigFile_Shop_Protect $protectConfig
210
     * @param DOMElement $shopXml
211
     * @return DOMElement
212
     */
213
    protected function mapProtect(Payone_Settings_Data_ConfigFile_Shop_Protect $protectConfig, DOMElement $shopXml)
214
    {
215
        $protectXml = $this->appendElement($shopXml, $protectConfig->getKey());
216
217
        $protectXml = $this->mapConsumerscore($protectConfig->getConsumerscore(), $protectXml);
218
        $protectXml = $this->mapAddresscheck($protectConfig->getAddresscheck(), $protectXml);
0 ignored issues
show
Unused Code introduced by
$protectXml is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
219
220
        return $shopXml;
221
    }
222
223
    /**
224
     * @param Payone_Settings_Data_ConfigFile_Shop_Misc $miscConfig
225
     * @param DOMElement $shopXml
226
     * @return DOMElement
227
     */
228
    protected function mapMisc(Payone_Settings_Data_ConfigFile_Shop_Misc $miscConfig, DOMElement $shopXml)
229
    {
230
        $miscXml = $this->appendElement($shopXml, $miscConfig->getKey());
231
232
        $this->addTransactionstatusForwarding($miscConfig, $miscXml);
233
        $this->addChild($miscXml, $miscConfig, 'shipping_costs');
234
        return $shopXml;
235
236
    }
237
238
    /**
239
     * @param Payone_Settings_Data_ConfigFile_Protect_Consumerscore $consumerscoreConfig
240
     * @param DOMElement $protectXml
241
     * @return DOMElement
242
     */
243 View Code Duplication
    protected function mapConsumerscore(Payone_Settings_Data_ConfigFile_Protect_Consumerscore $consumerscoreConfig, DOMElement $protectXml)
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...
244
    {
245
        $consumerscoreXml = $this->appendElement($protectXml, $consumerscoreConfig->getKey());
246
247
        $this->addChild($consumerscoreXml, $consumerscoreConfig, 'active');
248
        $this->addChild($consumerscoreXml, $consumerscoreConfig, 'mode');
249
        $this->addChild($consumerscoreXml, $consumerscoreConfig, 'min_order_total');
250
        $this->addChild($consumerscoreXml, $consumerscoreConfig, 'max_order_total');
251
        $this->addChild($consumerscoreXml, $consumerscoreConfig, 'addresscheck');
252
        $this->addChild($consumerscoreXml, $consumerscoreConfig, 'red');
253
        $this->addChild($consumerscoreXml, $consumerscoreConfig, 'yellow');
254
        $this->addChild($consumerscoreXml, $consumerscoreConfig, 'duetime');
255
256
        return $protectXml;
257
    }
258
259
    /**
260
     * @param Payone_Settings_Data_ConfigFile_Protect_Addresscheck $addresscheckConfig
261
     * @param DOMElement $protectXml
262
     * @return DOMElement
263
     */
264 View Code Duplication
    protected function mapAddresscheck(Payone_Settings_Data_ConfigFile_Protect_Addresscheck $addresscheckConfig, DOMElement $protectXml)
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...
265
    {
266
        $addresscheckXml = $this->appendElement($protectXml, $addresscheckConfig->getKey());
267
268
        $this->addChild($addresscheckXml, $addresscheckConfig, 'active');
269
        $this->addChild($addresscheckXml, $addresscheckConfig, 'mode');
270
        $this->addChild($addresscheckXml, $addresscheckConfig, 'min_order_total');
271
        $this->addChild($addresscheckXml, $addresscheckConfig, 'max_order_total');
272
        $this->addChild($addresscheckXml, $addresscheckConfig, 'checkbilling');
273
        $this->addChild($addresscheckXml, $addresscheckConfig, 'checkshipping');
274
        $this->addChild($addresscheckXml, $addresscheckConfig, 'personstatusmapping');
275
276
        return $protectXml;
277
    }
278
279
    /**
280
     * @param Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig
281
     * @param DOMElement $globalXml
282
     * @return DOMElement
283
     */
284
    protected function mapParameterInvoice(Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig, DOMElement $globalXml)
285
    {
286
        $parameterInvoice = $globalConfig->getParameterInvoice();
287
        $parameterInvoiceXml = $this->appendElement($globalXml, 'parameter_invoice');
288
289
        $this->appendElement($parameterInvoiceXml, 'invoice_appendix', $parameterInvoice['invoice_appendix'], true);
290
        $this->appendElement($parameterInvoiceXml, 'invoice_appendix_refund', $parameterInvoice['invoice_appendix_refund'], true);
291
        $this->appendElement($parameterInvoiceXml, 'pdf_download_enabled', $parameterInvoice['pdf_download_enabled']);
292
        $this->appendElement($parameterInvoiceXml, 'transmit_enabled', $parameterInvoice['transmit_enabled']);
293
294
        return $globalXml;
295
    }
296
297
    /**
298
     * @param Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig
299
     * @param DOMElement $globalXml
300
     * @return DOMElement
301
     */
302
    protected function mapPaymentCreditcard(Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig, DOMElement $globalXml)
303
    {
304
        $paymentCreditcard = $globalConfig->getPaymentCreditcard();
305
        $paymentCreditcardXml = $this->appendElement($globalXml, 'payment_creditcard');
306
307
        $this->appendElement($paymentCreditcardXml, 'min_validity_period', $paymentCreditcard['min_validity_period']);
308
309
        return $globalXml;
310
    }
311
312
    /**
313
     * @param Payone_Settings_Data_ConfigFile_Shop_Misc $miscConfig
314
     * @param DOMElement $miscXml
315
     */
316
    public function addTransactionstatusForwarding(Payone_Settings_Data_ConfigFile_Shop_Misc $miscConfig, DOMElement $miscXml)
317
    {
318
        $tasForwarding = $miscConfig->getTransactionstatusforwarding();
319
        $tasXml = $this->appendElement($miscXml, $tasForwarding->getKey());
320
321
        foreach ($tasForwarding->getTransactionstatusForwarding() as $keyTas => $config) {
322
            $configNode = $this->appendElement($tasXml, 'config');
323
324
            foreach ($config as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $config of type object<Payone_Settings_D...actionstatusForwarding> is not traversable.
Loading history...
325
                $configNode->setAttribute($key, $value);
326
            }
327
        }
328
    }
329
330
    /**
331
     * @param Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig
332
     * @param DOMElement $globalXml
333
     */
334
    public function addStatusMapping(Payone_Settings_Data_ConfigFile_Shop_Global $globalConfig, DOMElement $globalXml)
335
    {
336
        $statusMapping = $globalConfig->getStatusMapping();
337
        $tasXml = $this->appendElement($globalXml, $statusMapping->getKey());
338
339
        foreach ($statusMapping->getStatusMapping() as $keyStatusMapping => $valueStatusMapping) {
340
            $parent = $this->appendElement($tasXml, $keyStatusMapping);
341
342
            foreach ($valueStatusMapping as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $valueStatusMapping of type object<Payone_Settings_D...e_Global_StatusMapping> is not traversable.
Loading history...
343
                $mapNode = $this->appendElement($parent, 'map');
344
345
                $this->addAttribute($mapNode, $key, $value);
346
            }
347
        }
348
    }
349
350
    /**
351
     * @param DOMElement $cleatringTypeNode
352
     * @param Payone_Settings_Data_ConfigFile_PaymentMethod_Abstract $valueClearingType
353
     */
354
    public function addTypesOrGlobalInfo(DOMElement $cleatringTypeNode, Payone_Settings_Data_ConfigFile_PaymentMethod_Abstract $valueClearingType)
355
    {
356
        if ($valueClearingType->getTypes() !== NULL && $valueClearingType->getTypes() !== FALSE) {
357
            if ($valueClearingType instanceof Payone_Settings_Data_ConfigFile_PaymentMethod_Creditcard) {
358
                /** @var $valueClearingType Payone_Settings_Data_ConfigFile_PaymentMethod_Creditcard */
359
                $this->addChild($cleatringTypeNode, $valueClearingType, 'cvc2');
360
            }
361
362
            $this->addChild($cleatringTypeNode, $valueClearingType, 'types');
363
        }
364
365
        $this->addGlobal($cleatringTypeNode, $valueClearingType);
366
    }
367
368
    public function addGlobal($parent, $type)
369
    {
370
371
        $this->addChild($parent, $type, 'active');
372
        // Currently not in use
373
        //$this->addChild($parent, $type, 'neworderstatus');
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
374
        $this->addChild($parent, $type, 'countries');
375
        $this->addChild($parent, $type, 'authorization');
376
        $this->addChild($parent, $type, 'mode');
377
    }
378
379
    /**
380
     * @param DOMElement $cleatringTypeNode
381
     * @param Payone_Settings_Data_ConfigFile_PaymentMethod_Abstract $valueClearingType
382
     */
383
    public function addFeeConfig(DOMElement $cleatringTypeNode, Payone_Settings_Data_ConfigFile_PaymentMethod_Abstract $valueClearingType)
384
    {
385
        $feeConfig = $valueClearingType->getFeeConfig();
386
        if (!empty($feeConfig)) {
387
            $feeConfigNode = $this->appendElement($cleatringTypeNode, 'fee_config');
388
389
            foreach ($feeConfig as $keyFeeConfig => $valueFeeConfig) {
390
                if (array_key_exists('value', $valueFeeConfig) && array_key_exists('attribute', $valueFeeConfig)) {
391
                    $feeNode = $this->appendElement($feeConfigNode, 'fee', $valueFeeConfig['value']);
392
                    foreach ($valueFeeConfig['attribute'] as $keyFee => $valueFee) {
393
                        $feeNode->setAttribute($keyFee, $valueFee);
394
                    }
395
                }
396
            }
397
        }
398
    }
399
400
    /**
401
     * @param DOMElement $parent
402
     * @param $object
403
     * @param $property
404
     * @param bool $withCdata
405
     * @return DOMElement
406
     */
407
    protected function addChild(DOMElement $parent, $object, $property, $withCdata = false)
408
    {
409
        $getter = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
410
        $data = $object->$getter();
411
        $child = $parent;
412
        if (is_array($data)) {
413
            $parentNode = $this->appendElement($parent, $property);
414
            foreach ($data as $key => $value) {
415
                $child = $this->appendElement($parentNode, $key, $value, $withCdata);
416
            }
417
        }
418
        else {
419
            if (isset($data)) {
420
                $child = $this->appendElement($parent, $property, $data, $withCdata);
421
            }
422
        }
423
424
        return $child;
425
    }
426
427
    /**
428
     * @param DOMElement $mapNode
429
     * @param $name
430
     * @param $value
431
     * @return DOMElement
432
     */
433
    protected function addAttribute(DOMElement $mapNode, $name, $value)
434
    {
435
        if (is_array($value)) {
436
            foreach ($value as $key => $data) {
437
                $mapNode->setAttribute($key, $data);
438
            }
439
        }
440
        else {
441
            if (!empty($data)) {
0 ignored issues
show
Bug introduced by
The variable $data seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
442
                $mapNode->setAttribute($name, $value);
443
            }
444
        }
445
446
        return $mapNode;
447
    }
448
449
    /**
450
     * @param DOMElement|DOMDocument $parent
451
     * @param $key
452
     * @param $value
453
     * @param bool $asCdata
454
     * @return DOMElement
455
     */
456
    protected function appendElement( $parent, $key, $value = null, $asCdata = false)
457
    {
458
        if($asCdata === true)
459
        {
460
            $cdata = $this->dom->createCDATASection($value);
461
            $child = $this->dom->createElement($key);
462
            $child->appendChild($cdata);
463
        }
464
        else
465
        {
466
            $child = $this->dom->createElement($key);
467
            if($value !== null)
468
            {
469
                $domValue = $this->dom->createTextNode($value);
470
                $child->appendChild($domValue);
471
            }
472
        }
473
474
        $parent->appendChild($child);
475
        return $child;
476
    }
477
}
478