PagSeguroPaymentParser::getData()   F
last analyzed

Complexity

Conditions 60
Paths > 20000

Size

Total Lines 197
Code Lines 103

Duplication

Lines 145
Ratio 73.6 %

Importance

Changes 0
Metric Value
cc 60
eloc 103
c 0
b 0
f 0
nc 254994432
nop 1
dl 145
loc 197
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 PagSeguroPaymentParser
26
 */
27
class PagSeguroPaymentParser extends PagSeguroServiceParser
28
{
29
30
    /***
31
     * @param $payment PagSeguroPaymentRequest
32
     * @return mixed
33
     */
34
    public static function getData($payment)
35
    {
36
37
        $data = null;
38
39
        // reference
40
        if ($payment->getReference() != null) {
41
            $data["reference"] = $payment->getReference();
42
        }
43
44
        // sender
45 View Code Duplication
        if ($payment->getSender() != null) {
46
47
            if ($payment->getSender()->getName() != null) {
48
                $data['senderName'] = $payment->getSender()->getName();
49
            }
50
            if ($payment->getSender()->getEmail() != null) {
51
                $data['senderEmail'] = $payment->getSender()->getEmail();
52
            }
53
54
            // phone
55
            if ($payment->getSender()->getPhone() != null) {
56
                if ($payment->getSender()->getPhone()->getAreaCode() != null) {
57
                    $data['senderAreaCode'] = $payment->getSender()->getPhone()->getAreaCode();
58
                }
59
                if ($payment->getSender()->getPhone()->getNumber() != null) {
60
                    $data['senderPhone'] = $payment->getSender()->getPhone()->getNumber();
61
                }
62
            }
63
64
            // documents
65
            /*** @var $document PagSeguroDocument */
66
            if ($payment->getSender()->getDocuments() != null) {
67
                $documents = $payment->getSender()->getDocuments();
68
                if (is_array($documents) && count($documents) == 1) {
69
                    foreach ($documents as $document) {
70
                        if (!is_null($document)) {
71
                            $data['senderCPF'] = $document->getValue();
72
                        }
73
                    }
74
                }
75
            }
76
77
             if ($payment->getSender()->getIP() != null) {
78
                $data['ip'] = $payment->getSender()->getIP();
79
            }
80
        }
81
82
        // currency
83
        if ($payment->getCurrency() != null) {
84
            $data['currency'] = $payment->getCurrency();
85
        }
86
87
        // items
88
        $items = $payment->getItems();
89 View Code Duplication
        if (count($items) > 0) {
90
91
            $i = 0;
92
93
            foreach ($items as $key => $value) {
94
                $i++;
95
                if ($items[$key]->getId() != null) {
96
                    $data["itemId$i"] = $items[$key]->getId();
97
                }
98
                if ($items[$key]->getDescription() != null) {
99
                    $data["itemDescription$i"] = $items[$key]->getDescription();
100
                }
101
                if ($items[$key]->getQuantity() != null) {
102
                    $data["itemQuantity$i"] = $items[$key]->getQuantity();
103
                }
104
                if ($items[$key]->getAmount() != null) {
105
                    $amount = PagSeguroHelper::decimalFormat($items[$key]->getAmount());
106
                    $data["itemAmount$i"] = $amount;
107
                }
108
                if ($items[$key]->getWeight() != null) {
109
                    $data["itemWeight$i"] = $items[$key]->getWeight();
110
                }
111
                if ($items[$key]->getShippingCost() != null) {
112
                    $data["itemShippingCost$i"] = PagSeguroHelper::decimalFormat($items[$key]->getShippingCost());
113
                }
114
            }
115
        }
116
117
        // extraAmount
118
        if ($payment->getExtraAmount() != null) {
119
            $data['extraAmount'] = PagSeguroHelper::decimalFormat($payment->getExtraAmount());
120
        }
121
122
        // shipping
123 View Code Duplication
        if ($payment->getShipping() != null) {
124
125
            if ($payment->getShipping()->getType() != null && $payment->getShipping()->getType()->getValue() != null) {
126
                $data['shippingType'] = $payment->getShipping()->getType()->getValue();
127
            }
128
129
            if ($payment->getShipping()->getCost() != null && $payment->getShipping()->getCost() != null) {
130
                $data['shippingCost'] = PagSeguroHelper::decimalFormat($payment->getShipping()->getCost());
131
            }
132
133
            // address
134
            if ($payment->getShipping()->getAddress() != null) {
135
                if ($payment->getShipping()->getAddress()->getStreet() != null) {
136
                    $data['shippingAddressStreet'] = $payment->getShipping()->getAddress()->getStreet();
137
                }
138
                if ($payment->getShipping()->getAddress()->getNumber() != null) {
139
                    $data['shippingAddressNumber'] = $payment->getShipping()->getAddress()->getNumber();
140
                }
141
                if ($payment->getShipping()->getAddress()->getComplement() != null) {
142
                    $data['shippingAddressComplement'] = $payment->getShipping()->getAddress()->getComplement();
143
                }
144
                if ($payment->getShipping()->getAddress()->getCity() != null) {
145
                    $data['shippingAddressCity'] = $payment->getShipping()->getAddress()->getCity();
146
                }
147
                if ($payment->getShipping()->getAddress()->getState() != null) {
148
                    $data['shippingAddressState'] = $payment->getShipping()->getAddress()->getState();
149
                }
150
                if ($payment->getShipping()->getAddress()->getDistrict() != null) {
151
                    $data['shippingAddressDistrict'] = $payment->getShipping()->getAddress()->getDistrict();
152
                }
153
                if ($payment->getShipping()->getAddress()->getPostalCode() != null) {
154
                    $data['shippingAddressPostalCode'] = $payment->getShipping()->getAddress()->getPostalCode();
155
                }
156
                if ($payment->getShipping()->getAddress()->getCountry() != null) {
157
                    $data['shippingAddressCountry'] = $payment->getShipping()->getAddress()->getCountry();
158
                }
159
            }
160
        }
161
        // maxAge
162
        if ($payment->getMaxAge() != null) {
163
            $data['maxAge'] = $payment->getMaxAge();
164
        }
165
        // maxUses
166
        if ($payment->getMaxUses() != null) {
167
            $data['maxUses'] = $payment->getMaxUses();
168
        }
169
170
        // redirectURL
171
        if ($payment->getRedirectURL() != null) {
172
            $data['redirectURL'] = $payment->getRedirectURL();
173
        }
174
175
        // notificationURL
176
        if ($payment->getNotificationURL() != null) {
177
            $data['notificationURL'] = $payment->getNotificationURL();
178
        }
179
180
        // metadata
181 View Code Duplication
        if (count($payment->getMetaData()->getItems()) > 0) {
182
            $i = 0;
183
            foreach ($payment->getMetaData()->getItems() as $item) {
184
                if ($item instanceof PagSeguroMetaDataItem) {
185
                    if (!PagSeguroHelper::isEmpty($item->getKey()) && !PagSeguroHelper::isEmpty($item->getValue())) {
186
                        $i++;
187
                        $data['metadataItemKey' . $i] = $item->getKey();
188
                        $data['metadataItemValue' . $i] = $item->getValue();
189
190
                        if (!PagSeguroHelper::isEmpty($item->getGroup())) {
191
                            $data['metadataItemGroup' . $i] = $item->getGroup();
192
                        }
193
                    }
194
                }
195
            }
196
        }
197
198
        // paymentMethodConfig
199 View Code Duplication
        if (count($payment->getPaymentMethodConfig()->getConfig()) > 0) {
200
            $i = 0;
201
            foreach ($payment->getPaymentMethodConfig()->getConfig() as $config) {
202
                if ($config instanceof PagSeguroPaymentMethodConfigItem) {
203
                    if (!PagSeguroHelper::isEmpty($config->getKey()) && !PagSeguroHelper::isEmpty($config->getValue())) {
204
                        $i++;
205
                        if (!PagSeguroHelper::isEmpty($config->getGroup())) {
206
                            $data['paymentMethodGroup' . $i] = $config->getGroup();
207
                        }
208
                        $data['paymentMethodConfigKey' . $i . "_1"] = $config->getKey();
209
                        $data['paymentMethodConfigValue' . $i . "_1"] = $config->getValue();
210
                    }
211
                }
212
            }
213
        }
214
215
        // parameter
216 View Code Duplication
        if (count($payment->getParameter()->getItems()) > 0) {
217
            foreach ($payment->getParameter()->getItems() as $item) {
218
                if ($item instanceof PagSeguroParameterItem) {
219
                    if (!PagSeguroHelper::isEmpty($item->getKey()) && !PagSeguroHelper::isEmpty($item->getValue())) {
220
                        if (!PagSeguroHelper::isEmpty($item->getGroup())) {
221
                            $data[$item->getKey() . '' . $item->getGroup()] = $item->getValue();
222
                        } else {
223
                            $data[$item->getKey()] = $item->getValue();
224
                        }
225
                    }
226
                }
227
            }
228
        }
229
        return $data;
230
    }
231
232
    /***
233
     * @param $str_xml
234
     * @return PagSeguroPaymentParserData Success
235
     */
236 View Code Duplication
    public static function readSuccessXml($str_xml)
237
    {
238
        $parser = new PagSeguroXmlParser($str_xml);
239
        $data = $parser->getResult('checkout');
240
        $PaymentParserData = new PagSeguroPaymentParserData();
241
        $PaymentParserData->setCode($data['code']);
242
        $PaymentParserData->setRegistrationDate($data['date']);
243
        return $PaymentParserData;
244
    }
245
246
    /***
247
     * @param $str_xml
248
     * @return parsed credit card brand
249
     */
250 View Code Duplication
     public static function readCCBRandXml($str_xml)
251
    {
252
        $parser = new PagSeguroXmlParser($str_xml);
253
        $PaymentParserData = new PagSeguroPaymentParserData();
254
        $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...
255
        $PaymentParserData->setRegistrationDate($data['date']);
256
        return $PaymentParserData;
257
    }
258
259
    /***
260
     * @param $str_xml
261
     * @return parsed transaction
262
     */
263 View Code Duplication
    public static function readTransactionXml($str_xml)
264
    {
265
        $parser = new PagSeguroXmlParser($str_xml);
266
        $data = $parser->getResult('transaction');
267
        $PaymentParserData = new PagSeguroPaymentParserData();
268
        $PaymentParserData->setCode($data['code']);
269
        $PaymentParserData->setRegistrationDate($data['date']);
270
        return $PaymentParserData;
271
    }
272
}
273