GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#62)
by Jelte
04:52
created

AbstractPaymentRequest   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 294
Duplicated Lines 12.93 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 41
c 5
b 1
f 2
lcom 1
cbo 1
dl 38
loc 294
rs 8.2769

23 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrderid() 0 10 3
A setOrderDescription() 0 4 1
A setCom() 7 7 2
A setAmount() 13 13 4
A setCurrency() 0 7 2
A setEmail() 10 10 3
A setOwnerAddress() 0 7 2
A setOwnerZip() 0 7 2
A setOwnerTown() 0 7 2
A setOwnerCountry() 0 4 1
A setOwnercty() 0 7 2
A setOwnerPhone() 0 4 1
A setOwnertelno() 0 7 2
A setFeedbackMessage() 0 4 1
A setComplus() 0 4 1
A setBrand() 0 9 2
A setPaymentMethod() 0 4 1
A setPm() 0 7 2
A setParamvar() 0 7 3
A setDynamicTemplateUri() 0 4 1
A setTp() 0 5 1
A setOperation() 8 8 2
getValidOperations() 0 1 ?

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like AbstractPaymentRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AbstractPaymentRequest, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * This file is part of the Marlon Ogone package.
4
 *
5
 * (c) Marlon BVBA <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Ogone;
12
13
use InvalidArgumentException;
14
15
abstract class AbstractPaymentRequest extends AbstractRequest
16
{
17
    const OPERATION_REQUEST_AUTHORIZATION = 'RES';
18
    const OPERATION_REQUEST_DIRECT_SALE = 'SAL';
19
    const OPERATION_REFUND = 'RFD';
20
    const OPERATION_REQUEST_PRE_AUTHORIZATION = 'PAU';
21
22
    protected $brandsmap = array(
23
        'Acceptgiro'            => 'Acceptgiro',
24
        'AIRPLUS'               => 'CreditCard',
25
        'American Express'      => 'CreditCard',
26
        'Aurora'                => 'CreditCard',
27
        'Aurore'                => 'CreditCard',
28
        'Bank transfer'         => 'Bank transfer',
29
        'BCMC'                  => 'CreditCard',
30
        'Belfius Direct Net'    => 'Belfius Direct Net',
31
        'Billy'                 => 'CreditCard',
32
        'cashU'                 => 'cashU',
33
        'CB'                    => 'CreditCard',
34
        'CBC Online'            => 'CBC Online',
35
        'CENTEA Online'         => 'CENTEA Online',
36
        'Cofinoga'              => 'CreditCard',
37
        'Dankort'               => 'CreditCard',
38
        'Dexia Direct Net'      => 'Dexia Direct Net',
39
        'Diners Club'           => 'CreditCard',
40
        'Direct Debits AT'      => 'Direct Debits AT',
41
        'Direct Debits DE'      => 'Direct Debits DE',
42
        'Direct Debits NL'      => 'Direct Debits NL',
43
        'eDankort'              => 'eDankort',
44
        'EPS'                   => 'EPS',
45
        'Fortis Pay Button'     => 'Fortis Pay Button',
46
        'giropay'               => 'giropay',
47
        'iDEAL'                 => 'iDEAL',
48
        'ING HomePay'           => 'ING HomePay',
49
        'InterSolve'            => 'InterSolve',
50
        'JCB'                   => 'CreditCard',
51
        'KBC Online'            => 'KBC Online',
52
        'Maestro'               => 'CreditCard',
53
        'MaestroUK'             => 'CreditCard',
54
        'MasterCard'            => 'CreditCard',
55
        'MiniTix'               => 'MiniTix',
56
        'MPASS'                 => 'MPASS',
57
        'NetReserve'            => 'CreditCard',
58
        'Payment on Delivery'   => 'Payment on Delivery',
59
        'PAYPAL'                => 'PAYPAL',
60
        'paysafecard'           => 'paysafecard',
61
        'PingPing'              => 'PingPing',
62
        'PostFinance + card'    => 'PostFinance Card',
63
        'PostFinance e-finance' => 'PostFinance e-finance',
64
        'PRIVILEGE'             => 'CreditCard',
65
        'Sofort Uberweisung'    => 'DirectEbanking',
66
        'Solo'                  => 'CreditCard',
67
        'TUNZ'                  => 'TUNZ',
68
        'UATP'                  => 'CreditCard',
69
        'UNEUROCOM'             => 'UNEUROCOM',
70
        'VISA'                  => 'CreditCard',
71
        'Wallie'                => 'Wallie',
72
    );
73
74
    /** Note this is public to allow easy modification, if need be. */
75
    public $allowedcurrencies = array(
76
        'AED',
77
        'ANG',
78
        'ARS',
79
        'AUD',
80
        'AWG',
81
        'BGN',
82
        'BRL',
83
        'BYR',
84
        'CAD',
85
        'CHF',
86
        'CNY',
87
        'CZK',
88
        'DKK',
89
        'EEK',
90
        'EGP',
91
        'EUR',
92
        'GBP',
93
        'GEL',
94
        'HKD',
95
        'HRK',
96
        'HUF',
97
        'ILS',
98
        'ISK',
99
        'JPY',
100
        'KRW',
101
        'LTL',
102
        'LVL',
103
        'MAD',
104
        'MXN',
105
        'MYR',
106
        'NOK',
107
        'NZD',
108
        'PLN',
109
        'RON',
110
        'RUB',
111
        'SEK',
112
        'SGD',
113
        'SKK',
114
        'THB',
115
        'TRY',
116
        'UAH',
117
        'USD',
118
        'XAF',
119
        'XOF',
120
        'XPF',
121
        'ZAR'
122
    );
123
124
    public function setOrderid($orderid)
125
    {
126
        if (strlen($orderid) > 30) {
127
            throw new InvalidArgumentException("Orderid cannot be longer than 30 characters");
128
        }
129
        if (preg_match('/[^a-zA-Z0-9_-]/', $orderid)) {
130
            throw new InvalidArgumentException("Order id cannot contain special characters");
131
        }
132
        $this->parameters['orderid'] = $orderid;
133
    }
134
135
    /** Friend alias for setCom() */
136
    public function setOrderDescription($orderDescription)
137
    {
138
        $this->setCom($orderDescription);
139
    }
140
141 View Code Duplication
    public function setCom($com)
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...
142
    {
143
        if (strlen($com) > 100) {
144
            throw new InvalidArgumentException("Order description cannot be longer than 100 characters");
145
        }
146
        $this->parameters['com'] = $com;
147
    }
148
149
    /**
150
     * Set amount in cents, eg EUR 12.34 is written as 1234
151
     */
152 View Code Duplication
    public function setAmount($amount)
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...
153
    {
154
        if (!is_int($amount)) {
155
            throw new InvalidArgumentException("Integer expected. Amount is always in cents");
156
        }
157
        if ($amount <= 0) {
158
            throw new InvalidArgumentException("Amount must be a positive number");
159
        }
160
        if ($amount >= 1.0E+15) {
161
            throw new InvalidArgumentException("Amount is too high");
162
        }
163
        $this->parameters['amount'] = $amount;
164
    }
165
166
    public function setCurrency($currency)
167
    {
168
        if (!in_array(strtoupper($currency), $this->allowedcurrencies)) {
169
            throw new InvalidArgumentException("Unknown currency");
170
        }
171
        $this->parameters['currency'] = $currency;
172
    }
173
174 View Code Duplication
    public function setEmail($email)
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...
175
    {
176
        if (strlen($email) > 50) {
177
            throw new InvalidArgumentException("Email is too long");
178
        }
179
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
180
            throw new InvalidArgumentException("Email is invalid");
181
        }
182
        $this->parameters['email'] = $email;
183
    }
184
185
    public function setOwnerAddress($owneraddress)
186
    {
187
        if (strlen($owneraddress) > 35) {
188
            throw new InvalidArgumentException("Owner address is too long");
189
        }
190
        $this->parameters['owneraddress'] = $owneraddress;
191
    }
192
193
    public function setOwnerZip($ownerzip)
194
    {
195
        if (strlen($ownerzip) > 10) {
196
            throw new InvalidArgumentException("Owner Zip is too long");
197
        }
198
        $this->parameters['ownerzip'] = $ownerzip;
199
    }
200
201
    public function setOwnerTown($ownertown)
202
    {
203
        if (strlen($ownertown) > 40) {
204
            throw new InvalidArgumentException("Owner town is too long");
205
        }
206
        $this->parameters['ownertown'] = $ownertown;
207
    }
208
209
    /**
210
     * Alias for setOwnercty
211
     *
212
     * @see http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm
213
     */
214
    public function setOwnerCountry($ownercountry)
215
    {
216
        $this->setOwnercty($ownercountry);
217
    }
218
219
    /**
220
     * @see http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm
221
     */
222
    public function setOwnercty($ownercty)
223
    {
224
        if (!preg_match('/^[A-Z]{2}$/', strtoupper($ownercty))) {
225
            throw new InvalidArgumentException("Illegal country code");
226
        }
227
        $this->parameters['ownercty'] = strtoupper($ownercty);
228
    }
229
230
    /** Alias for setOwnertelno() */
231
    public function setOwnerPhone($ownerphone)
232
    {
233
        $this->setOwnertelno($ownerphone);
234
    }
235
236
    public function setOwnertelno($ownertelno)
237
    {
238
        if (strlen($ownertelno) > 30) {
239
            throw new InvalidArgumentException("Owner phone is too long");
240
        }
241
        $this->parameters['ownertelno'] = $ownertelno;
242
    }
243
244
    /** Alias for setComplus() */
245
    public function setFeedbackMessage($feedbackMessage)
246
    {
247
        $this->setComplus($feedbackMessage);
248
    }
249
250
    public function setComplus($complus)
251
    {
252
        $this->parameters['complus'] = $complus;
253
    }
254
255
    public function setBrand($brand)
256
    {
257
        if (!array_key_exists($brand, $this->brandsmap)) {
258
            throw new InvalidArgumentException("Unknown Brand [$brand].");
259
        }
260
261
        $this->setPaymentMethod($this->brandsmap[$brand]);
262
        $this->parameters['brand'] = $brand;
263
    }
264
265
    public function setPaymentMethod($paymentMethod)
266
    {
267
        $this->setPm($paymentMethod);
268
    }
269
270
    public function setPm($pm)
271
    {
272
        if (!in_array($pm, $this->brandsmap)) {
273
            throw new InvalidArgumentException("Unknown Payment method [$pm].");
274
        }
275
        $this->parameters['pm'] = $pm;
276
    }
277
278
    public function setParamvar($paramvar)
279
    {
280
        if (strlen($paramvar) < 2 || strlen($paramvar) > 50) {
281
            throw new InvalidArgumentException("Paramvar must be between 2 and 50 characters in length");
282
        }
283
        $this->parameters['paramvar'] = $paramvar;
284
    }
285
286
    /** Alias for setTp */
287
    public function setDynamicTemplateUri($uri)
288
    {
289
        $this->setTp($uri);
290
    }
291
292
    public function setTp($tp)
293
    {
294
        $this->validateUri($tp);
295
        $this->parameters['tp'] = $tp;
296
    }
297
298 View Code Duplication
    public function setOperation($operation)
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...
299
    {
300
        if (!in_array($operation, $this->getValidOperations())) {
301
            throw new InvalidArgumentException("Invalid operation [$operation].");
302
        }
303
304
        $this->parameters['operation'] = $operation;
305
    }
306
307
    abstract protected function getValidOperations();
308
}
309