PagSeguroDirectPaymentParser::getData()   F
last analyzed

Complexity

Conditions 80
Paths > 20000

Size

Total Lines 280
Code Lines 143

Duplication

Lines 130
Ratio 46.43 %

Importance

Changes 0
Metric Value
cc 80
eloc 143
c 0
b 0
f 0
nc 4294967295
nop 1
dl 130
loc 280
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * 2007-2014 [PagSeguro Internet Ltda.]
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 *Licensed under the Apache License, Version 2.0 (the "License");
8
 *you may not use this file except in compliance with the License.
9
 *You may obtain a copy of the License at
10
 *
11
 *http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 *Unless required by applicable law or agreed to in writing, software
14
 *distributed under the License is distributed on an "AS IS" BASIS,
15
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 *See the License for the specific language governing permissions and
17
 *limitations under the License.
18
 *
19
 *  @author    PagSeguro Internet Ltda.
20
 *  @copyright 2007-2014 PagSeguro Internet Ltda.
21
 *  @license   http://www.apache.org/licenses/LICENSE-2.0
22
 */
23
24
/***
25
 * Class PagSeguroDirectPaymentParser
26
 */
27
class PagSeguroDirectPaymentParser extends PagSeguroServiceParser
28
{
29
30
    /***
31
     * @param $payment PagSeguroDirectPaymentRequest
32
     * @return mixed
33
     */
34
    public static function getData($payment)
35
    {
36
37
        $data = null;
38
39
        // paymentMode
40
        if ($payment->getPaymentMode() != null) {
41
            $data["paymentMode"] = $payment->getPaymentMode()->getValue();
42
        }
43
44
        // paymentMethod
45
        if ($payment->getPaymentMethod()->getPaymentMethod() != null) {
46
            $data["paymentMethod"] = $payment->getPaymentMethod()->getPaymentMethod();
47
        }
48
49
        // senderHash
50
        if ($payment->getSenderHash() != null) {
51
            $data["senderHash"] = $payment->getSenderHash();
52
        }
53
54
         // receiverEmail
55
        if ($payment->getReceiverEmail() != null) {
56
            $data["receiverEmail"] = $payment->getReceiverEmail();
57
        }
58
59
        // reference
60
        if ($payment->getReference() != null) {
61
            $data["reference"] = $payment->getReference();
62
        }
63
64
        // sender
65 View Code Duplication
        if ($payment->getSender() != null) {
66
67
            if ($payment->getSender()->getName() != null) {
68
                $data['senderName'] = $payment->getSender()->getName();
69
            }
70
            if ($payment->getSender()->getEmail() != null) {
71
                $data['senderEmail'] = $payment->getSender()->getEmail();
72
            }
73
74
            // phone
75
            if ($payment->getSender()->getPhone() != null) {
76
                if ($payment->getSender()->getPhone()->getAreaCode() != null) {
77
                    $data['senderAreaCode'] = $payment->getSender()->getPhone()->getAreaCode();
78
                }
79
                if ($payment->getSender()->getPhone()->getNumber() != null) {
80
                    $data['senderPhone'] = $payment->getSender()->getPhone()->getNumber();
81
                }
82
            }
83
84
            // documents
85
            /*** @var $document PagSeguroDocument */
86
            if ($payment->getSender()->getDocuments() != null) {
87
                $documents = $payment->getSender()->getDocuments();
88
                if (is_array($documents) && count($documents) == 1) {
89
                    foreach ($documents as $document) {
90
                        if (!is_null($document)) {
91
                            $data['senderCPF'] = $document->getValue();
92
                        }
93
                    }
94
                }
95
            }
96
            // ip
97
             if ($payment->getSender()->getIP() != null) {
98
                $data['ip'] = $payment->getSender()->getIP();
99
            }
100
        }
101
102
        // currency
103
        if ($payment->getCurrency() != null) {
104
            $data['currency'] = $payment->getCurrency();
105
        }
106
107
        // items
108
        $items = $payment->getItems();
109 View Code Duplication
        if (count($items) > 0) {
110
111
            $i = 0;
112
113
            foreach ($items as $key => $value) {
114
                $i++;
115
                if ($items[$key]->getId() != null) {
116
                    $data["itemId$i"] = $items[$key]->getId();
117
                }
118
                if ($items[$key]->getDescription() != null) {
119
                    $data["itemDescription$i"] = $items[$key]->getDescription();
120
                }
121
                if ($items[$key]->getQuantity() != null) {
122
                    $data["itemQuantity$i"] = $items[$key]->getQuantity();
123
                }
124
                if ($items[$key]->getAmount() != null) {
125
                    $amount = PagSeguroHelper::decimalFormat($items[$key]->getAmount());
126
                    $data["itemAmount$i"] = $amount;
127
                }
128
                if ($items[$key]->getWeight() != null) {
129
                    $data["itemWeight$i"] = $items[$key]->getWeight();
130
                }
131
                if ($items[$key]->getShippingCost() != null) {
132
                    $data["itemShippingCost$i"] = PagSeguroHelper::decimalFormat($items[$key]->getShippingCost());
133
                }
134
            }
135
        }
136
137
        // extraAmount
138
        if ($payment->getExtraAmount() != null) {
139
            $data['extraAmount'] = PagSeguroHelper::decimalFormat($payment->getExtraAmount());
140
        }
141
142
        // shipping
143 View Code Duplication
        if ($payment->getShipping() != null) {
144
145
            if ($payment->getShipping()->getType() != null && $payment->getShipping()->getType()->getValue() != null) {
146
                $data['shippingType'] = $payment->getShipping()->getType()->getValue();
147
            }
148
149
            if ($payment->getShipping()->getCost() != null && $payment->getShipping()->getCost() != null) {
150
                $data['shippingCost'] = PagSeguroHelper::decimalFormat($payment->getShipping()->getCost());
151
            }
152
153
            // address
154
            if ($payment->getShipping()->getAddress() != null) {
155
                if ($payment->getShipping()->getAddress()->getStreet() != null) {
156
                    $data['shippingAddressStreet'] = $payment->getShipping()->getAddress()->getStreet();
157
                }
158
                if ($payment->getShipping()->getAddress()->getNumber() != null) {
159
                    $data['shippingAddressNumber'] = $payment->getShipping()->getAddress()->getNumber();
160
                }
161
                if ($payment->getShipping()->getAddress()->getComplement() != null) {
162
                    $data['shippingAddressComplement'] = $payment->getShipping()->getAddress()->getComplement();
163
                }
164
                if ($payment->getShipping()->getAddress()->getCity() != null) {
165
                    $data['shippingAddressCity'] = $payment->getShipping()->getAddress()->getCity();
166
                }
167
                if ($payment->getShipping()->getAddress()->getState() != null) {
168
                    $data['shippingAddressState'] = $payment->getShipping()->getAddress()->getState();
169
                }
170
                if ($payment->getShipping()->getAddress()->getDistrict() != null) {
171
                    $data['shippingAddressDistrict'] = $payment->getShipping()->getAddress()->getDistrict();
172
                }
173
                if ($payment->getShipping()->getAddress()->getPostalCode() != null) {
174
                    $data['shippingAddressPostalCode'] = $payment->getShipping()->getAddress()->getPostalCode();
175
                }
176
                if ($payment->getShipping()->getAddress()->getCountry() != null) {
177
                    $data['shippingAddressCountry'] = $payment->getShipping()->getAddress()->getCountry();
178
                }
179
            }
180
        }
181
182
        // Bank name
183
        if ($payment->getOnlineDebit() != null) {
184
            $data["bankName"] = $payment->getOnlineDebit()->getBankName();
185
        }
186
187
        //Credit Card
188
        if ($payment->getCreditCard() != null) {
189
            
190
            //Token
191
            if ($payment->getCreditCard()->getToken() != null) {
192
                $data['creditCardToken'] = $payment->getCreditCard()->getToken();
193
            }
194
195
            //Installments
196
            if ($payment->getCreditCard()->getInstallment() != null) {
197
                $installment = $payment->getCreditCard()->getInstallment();
198
                if ($installment->getQuantity() != null && $installment->getValue()) {
199
                    $data['installmentQuantity'] = $installment->getQuantity();
200
                    $data['installmentValue']    = PagSeguroHelper::decimalFormat($installment->getValue());
201
                }
202
            }
203
204
            //Holder
205
            if ($payment->getCreditCard()->getHolder() != null) {
206
                $holder = $payment->getCreditCard()->getHolder();
207
                if ($holder->getName() != null) {
208
                    $data['creditCardHolderName'] = $holder->getName();
209
                }
210
                 // documents
211
                /*** @var $document PagSeguroDocument */
212
                if ($payment->getCreditCard()->getHolder()->getDocuments() != null) {
213
                    $documents = $payment->getCreditCard()->getHolder()->getDocuments();
214
                        $data['creditCardHolderCPF'] = $documents->getValue();
215
                }
216
                if ($holder->getBirthDate() != null) {
217
                    $data['creditCardHolderBirthDate'] = $holder->getBirthDate();
218
                }
219
                // phone
220
                if ($holder->getPhone() != null) {
221
                    if ($holder->getPhone()->getAreaCode() != null) {
222
                        $data['creditCardHolderAreaCode'] = $holder->getPhone()->getAreaCode();
223
                    }
224
                    if ($holder->getPhone()->getNumber() != null) {
225
                        $data['creditCardHolderPhone'] = $holder->getPhone()->getNumber();
226
                    }
227
                }
228
            }
229
230
            //Billing Address
231
            if ($payment->getCreditCard()->getBilling() != null) {
232
                $billingAddress = $payment->getCreditCard()->getBilling()->getAddress();
233
                if ($billingAddress->getStreet() != null) {
234
                    $data['billingAddressStreet'] = $billingAddress->getStreet();
235
                }
236
                if ($billingAddress->getNumber() != null) {
237
                    $data['billingAddressNumber'] = $billingAddress->getNumber();
238
                }
239
                if ($billingAddress->getComplement() != null) {
240
                    $data['billingAddressComplement'] = $billingAddress->getComplement();
241
                }
242
                if ($billingAddress->getCity() != null) {
243
                    $data['billingAddressCity'] = $billingAddress->getCity();
244
                }
245
                if ($billingAddress->getState() != null) {
246
                    $data['billingAddressState'] = $billingAddress->getState();
247
                }
248
                if ($billingAddress->getDistrict() != null) {
249
                    $data['billingAddressDistrict'] = $billingAddress->getDistrict();
250
                }
251
                if ($billingAddress->getPostalCode() != null) {
252
                    $data['billingAddressPostalCode'] = $billingAddress->getPostalCode();
253
                }
254
                if ($billingAddress->getCountry() != null) {
255
                    $data['billingAddressCountry'] = $billingAddress->getCountry();
256
                }
257
            }
258
259
        }
260
261
        // maxAge
262
        if ($payment->getMaxAge() != null) {
263
            $data['maxAge'] = $payment->getMaxAge();
264
        }
265
        // maxUses
266
        if ($payment->getMaxUses() != null) {
267
            $data['maxUses'] = $payment->getMaxUses();
268
        }
269
270
        // redirectURL
271
        if ($payment->getRedirectURL() != null) {
272
            $data['redirectURL'] = $payment->getRedirectURL();
273
        }
274
275
        // notificationURL
276
        if ($payment->getNotificationURL() != null) {
277
            $data['notificationURL'] = $payment->getNotificationURL();
278
        }
279
280
        // metadata
281 View Code Duplication
        if (count($payment->getMetaData()->getItems()) > 0) {
282
            $i = 0;
283
            foreach ($payment->getMetaData()->getItems() as $item) {
284
                if ($item instanceof PagSeguroMetaDataItem) {
285
                    if (!PagSeguroHelper::isEmpty($item->getKey()) && !PagSeguroHelper::isEmpty($item->getValue())) {
286
                        $i++;
287
                        $data['metadataItemKey' . $i] = $item->getKey();
288
                        $data['metadataItemValue' . $i] = $item->getValue();
289
290
                        if (!PagSeguroHelper::isEmpty($item->getGroup())) {
291
                            $data['metadataItemGroup' . $i] = $item->getGroup();
292
                        }
293
                    }
294
                }
295
            }
296
        }
297
298
        // parameter
299 View Code Duplication
        if (count($payment->getParameter()->getItems()) > 0) {
300
            foreach ($payment->getParameter()->getItems() as $item) {
301
                if ($item instanceof PagSeguroParameterItem) {
302
                    if (!PagSeguroHelper::isEmpty($item->getKey()) && !PagSeguroHelper::isEmpty($item->getValue())) {
303
                        if (!PagSeguroHelper::isEmpty($item->getGroup())) {
304
                            $data[$item->getKey() . '' . $item->getGroup()] = $item->getValue();
305
                        } else {
306
                            $data[$item->getKey()] = $item->getValue();
307
                        }
308
                    }
309
                }
310
            }
311
        }
312
        return $data;   
313
    }
314
315
    /***
316
     * @param $str_xml
317
     * @return PagSeguroDirectPaymentData Success
318
     */
319 View Code Duplication
    public static function readSuccessXml($str_xml)
320
    {
321
        $parser = new PagSeguroXmlParser($str_xml);
322
        $data = $parser->getResult('checkout');
323
        $PaymentParserData = new PagSeguroPaymentParserData();
324
        $PaymentParserData->setCode($data['code']);
325
        $PaymentParserData->setRegistrationDate($data['date']);
326
        return $PaymentParserData;
327
    }
328
329
    /***
330
     * @param $str_xml
331
     * @return parsed credit card brand
332
     */
333 View Code Duplication
     public static function readCCBRandXml($str_xml)
334
    {
335
        $parser = new PagSeguroXmlParser($str_xml);
336
        $PaymentParserData = new PagSeguroPaymentParserData();
337
        $PaymentParserData->setCode($data['code']);
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
338
        $PaymentParserData->setRegistrationDate($data['date']);
339
        return $PaymentParserData;
340
    }
341
342
    /***
343
     * @param $str_xml
344
     * @return parsed transaction
345
     */
346 View Code Duplication
    public static function readTransactionXml($str_xml)
347
    {
348
        $parser = new PagSeguroXmlParser($str_xml);
349
        $data = $parser->getResult('transaction');
350
        $PaymentParserData = new PagSeguroPaymentParserData();
351
        $PaymentParserData->setCode($data['code']);
352
        $PaymentParserData->setRegistrationDate($data['date']);
353
        return $PaymentParserData;
354
    }
355
}
356