Completed
Push — master ( f0e377...606176 )
by recca
02:17
created

EcpayLogisticsApi   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 382
Duplicated Lines 12.3 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 32.06%

Importance

Changes 0
Metric Value
wmc 21
c 0
b 0
f 0
lcom 1
cbo 2
dl 47
loc 382
ccs 67
cts 209
cp 0.3206
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createCvsMapTransaction() 0 20 2
A createPrintTradeTransaction() 0 17 1
A createPrintUnimartC2CBillTransaction() 19 19 1
A createPrintFamilyC2CBillTransaction() 18 18 1
A __construct() 10 10 2
A getApiEndpoint() 0 4 1
C createTransaction() 0 78 7
A refundTransaction() 0 67 3
B cancelTransaction() 0 27 2
A getTransactionData() 0 16 1

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace PayumTW\Ecpay;
4
5
use Http\Message\MessageFactory;
6
use Payum\Core\HttpClientInterface;
7
use PayumTW\Ecpay\Bridge\Ecpay\Device;
8
use PayumTW\Ecpay\Bridge\Ecpay\Distance;
9
use PayumTW\Ecpay\Bridge\Ecpay\Temperature;
10
use PayumTW\Ecpay\Bridge\Ecpay\IsCollection;
11
use PayumTW\Ecpay\Bridge\Ecpay\LogisticsType;
12
use PayumTW\Ecpay\Bridge\Ecpay\Specification;
13
use PayumTW\Ecpay\Bridge\Ecpay\EcpayLogistics;
14
use PayumTW\Ecpay\Bridge\Ecpay\LogisticsSubType;
15
use PayumTW\Ecpay\Bridge\Ecpay\ScheduledPickupTime;
16
17
class EcpayLogisticsApi extends Api
18
{
19
    /**
20
     * $client.
21
     *
22
     * @var \Payum\Core\HttpClientInterface
23
     */
24
    protected $client;
25
26
    /**
27
     * MessageFactory.
28
     *
29
     * @var MessageFactory
30
     */
31
    protected $messageFactory;
32
33
    /**
34
     * $options.
35
     *
36
     * @var array
37
     */
38
    protected $options = [];
39
40
    /**
41
     * $sdk.
42
     *
43
     * @var \PayumTW\Ecpay\Bridge\Ecpay\EcpayLogistics
44
     */
45
    protected $sdk;
46
47
    /**
48
     * @var array
49
     */
50
    protected $code = [];
51
52
    /**
53
     * @param array $options
54
     * @param \Payum\Core\HttpClientInterface $client
55
     * @param MessageFactory $messageFactory
56
     * @param \PayumTW\Ecpay\Bridge\Ecpay\EcpayLogistics $sdk
57
     *
58
     * @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid
59
     */
60 3 View Code Duplication
    public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory, EcpayLogistics $sdk = null)
1 ignored issue
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...
61
    {
62 3
        $this->options = $options;
63 3
        $this->client = $client;
64 3
        $this->messageFactory = $messageFactory;
65 3
        $this->sdk = $sdk ?: new EcpayLogistics();
66 3
        $this->sdk->HashKey = $this->options['HashKey'];
67 3
        $this->sdk->HashIV = $this->options['HashIV'];
68 3
        $this->sdk->Send['MerchantID'] = $this->options['MerchantID'];
69 3
    }
70
71
    /**
72
     * getApiEndpoint.
73
     *
74
     * @return string
75
     */
76
    public function getApiEndpoint($name = 'AioCheckOut')
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        return $this->sdk->ServiceURL;
79
    }
80
81
    /**
82
     * createTransaction.
83
     *
84
     * @param array $params
85
     * @return array
86
     */
87 1
    public function createCvsMapTransaction(array $params)
88
    {
89 1
        $this->sdk->Send = array_merge($this->sdk->Send, [
90 1
            'MerchantTradeNo' => '',
91 1
            'LogisticsSubType' => LogisticsSubType::UNIMART,
92 1
            'IsCollection' => IsCollection::NO,
93 1
            'ServerReplyURL' => '',
94 1
            'ExtraData' => '',
95 1
            'Device' => $this->isMobile() ? Device::MOBILE : Device::PC,
96 1
        ]);
97
98 1
        $this->sdk->Send = array_replace(
99 1
            $this->sdk->Send,
100 1
            array_intersect_key($params, $this->sdk->Send)
101 1
        );
102
103 1
        return $this->sdk->formToArray(
104 1
            $this->sdk->CvsMap()
105 1
        );
106
    }
107
108
    /**
109
     * 產生托運單(宅配)/一段標(超商取貨).
110
     *
111
     * @param array $params
112
     * @return array
113
     */
114
    public function createPrintTradeTransaction(array $params)
115
    {
116
        // 參數初始化
117
        $this->sdk->Send = array_merge($this->sdk->Send, [
118
            'AllPayLogisticsID' => '',
119
            'PlatformID'        => '',
120
        ]);
121
122
        $this->sdk->Send = array_replace(
123
            $this->sdk->Send,
124
            array_intersect_key($params, $this->sdk->Send)
125
        );
126
127
        return $this->sdk->formToArray(
128
            $this->sdk->PrintTradeDoc()
129
        );
130
    }
131
132
    /**
133
     * 列印繳款單(統一超商C2C).
134
     *
135
     * @param array $params
136
     * @return array
137
     */
138 View Code Duplication
    public function createPrintUnimartC2CBillTransaction(array $params)
1 ignored issue
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...
139
    {
140
        // 參數初始化
141
        $this->sdk->Send = array_merge($this->sdk->Send, [
142
            'AllPayLogisticsID' => '',
143
            'CVSPaymentNo'      => '',
144
            'CVSValidationNo'   => '',
145
            'PlatformID'        => '',
146
        ]);
147
148
        $this->sdk->Send = array_replace(
149
            $this->sdk->Send,
150
            array_intersect_key($params, $this->sdk->Send)
151
        );
152
153
        return $this->sdk->formToArray(
154
            $this->sdk->PrintUnimartC2CBill()
155
        );
156
    }
157
158
    /**
159
     * 全家列印小白單(全家超商C2C).
160
     *
161
     * @param array $params
162
     * @return array
163
     */
164 View Code Duplication
    public function createPrintFamilyC2CBillTransaction(array $params)
1 ignored issue
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...
165
    {
166
        // 參數初始化
167
        $this->sdk->Send = array_merge($this->sdk->Send, [
168
            'AllPayLogisticsID' => '',
169
            'CVSPaymentNo'      => '',
170
            'PlatformID'        => '',
171
        ]);
172
173
        $this->sdk->Send = array_replace(
174
            $this->sdk->Send,
175
            array_intersect_key($params, $this->sdk->Send)
176
        );
177
178
        return $this->sdk->formToArray(
179
            $this->sdk->PrintUnimartC2CBill()
180
        );
181
    }
182
183
    /**
184
     * createTransaction.
185
     *
186
     * @param array $params
187
     * @return array
188
     */
189 1
    public function createTransaction(array $params)
190
    {
191 1
        $this->sdk->Send = array_merge($this->sdk->Send, [
192 1
            'MerchantTradeNo' => '',
193 1
            'MerchantTradeDate' => date('Y/m/d H:i:s'),
194 1
            'LogisticsType' => '',
195 1
            'LogisticsSubType' => LogisticsSubType::UNIMART,
196 1
            'GoodsAmount' => 0,
197 1
            'CollectionAmount' => 0,
198 1
            'IsCollection' => IsCollection::NO,
199 1
            'GoodsName' => '',
200 1
            'SenderName' => '',
201 1
            'SenderPhone' => '',
202 1
            'SenderCellPhone' => '',
203 1
            'ReceiverName' => '',
204 1
            'ReceiverPhone' => '',
205 1
            'ReceiverCellPhone' => '',
206 1
            'ReceiverEmail' => '',
207 1
            'TradeDesc' => '',
208 1
            'ServerReplyURL' => '',
209 1
            'LogisticsC2CReplyURL' => '',
210 1
            'Remark' => '',
211 1
            'PlatformID' => '',
212 1
        ]);
213
214 1
        $this->sdk->SendExtend = [];
1 ignored issue
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type string of property $SendExtend.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
215
216 1
        $this->sdk->Send = array_replace(
217 1
            $this->sdk->Send,
218 1
            array_intersect_key($params, $this->sdk->Send)
219 1
        );
220
221 1
        $this->sdk->Send['GoodsAmount'] = (int) $this->sdk->Send['GoodsAmount'];
222 1
        $this->sdk->Send['CollectionAmount'] = (int) $this->sdk->Send['CollectionAmount'];
223
224 1
        if (empty($this->sdk->Send['LogisticsType']) === true) {
225
            $this->sdk->Send['LogisticsType'] = LogisticsType::CVS;
226
            switch ($this->sdk->Send['LogisticsSubType']) {
227
                case LogisticsSubType::TCAT:
228
                    $this->sdk->Send['LogisticsType'] = LogisticsType::HOME;
229
                    break;
230
            }
231
        }
232
233 1
        if ($this->sdk->Send['IsCollection'] === IsCollection::NO) {
234
            $this->sdk->Send['CollectionAmount'] = 0;
235 1
        } elseif (isset($this->sdk->Send['CollectionAmount']) === false) {
236
            $this->sdk->Send['CollectionAmount'] = (int) $this->sdk->Send['GoodsAmount'];
237
        }
238
239 1
        switch ($this->sdk->Send['LogisticsType']) {
240 1
            case LogisticsType::HOME:
241
                $this->sdk->SendExtend = array_merge($this->sdk->SendExtend, [
1 ignored issue
show
Documentation Bug introduced by
It seems like array_merge($this->sdk->...edDeliveryTime' => '')) of type array is incompatible with the declared type string of property $SendExtend.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
242
                    'SenderZipCode' => '',
243
                    'SenderAddress' => '',
244
                    'ReceiverZipCode' => '',
245
                    'ReceiverAddress' => '',
246
                    'Temperature' => '',
247
                    'Distance' => '',
248
                    'Specification' => '',
249
                    'ScheduledDeliveryTime' => '',
250
                ]);
251
                break;
252 1
            case LogisticsType::CVS:
253
                $this->sdk->SendExtend = array_merge($this->sdk->SendExtend, [
1 ignored issue
show
Documentation Bug introduced by
It seems like array_merge($this->sdk->...'ReturnStoreID' => '')) of type array is incompatible with the declared type string of property $SendExtend.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
254
                    'ReceiverStoreID' => '',
255
                    'ReturnStoreID' => '',
256
                ]);
257
                break;
258 1
        }
259
260 1
        $this->sdk->SendExtend = array_replace(
1 ignored issue
show
Documentation Bug introduced by
It seems like array_replace($this->sdk...this->sdk->SendExtend)) of type array is incompatible with the declared type string of property $SendExtend.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
261 1
            $this->sdk->SendExtend,
262 1
            array_intersect_key($params, $this->sdk->SendExtend)
263 1
        );
264
265 1
        return $this->sdk->BGCreateShippingOrder();
266
    }
267
268
    /**
269
     * refundTransaction.
270
     *
271
     * @param array $params
272
     * @return array
273
     */
274
    public function refundTransaction($params)
275
    {
276
        if ($params['LogisticsSubType'] === LogisticsSubType::TCAT) {
277
            // 宅配逆物流訂單產生
278
            $method = 'CreateHomeReturnOrder';
279
            $supportedParams = [
280
                'AllPayLogisticsID'     => '',
281
                'LogisticsSubType'      => '',
282
                'ServerReplyURL'        => '',
283
                'SenderName'            => '',
284
                'SenderPhone'           => '',
285
                'SenderCellPhone'       => '',
286
                'SenderZipCode'         => '',
287
                'SenderAddress'         => '',
288
                'ReceiverName'          => '',
289
                'ReceiverPhone'         => '',
290
                'ReceiverCellPhone'     => '',
291
                'ReceiverZipCode'       => '',
292
                'ReceiverAddress'       => '',
293
                'GoodsAmount'           => '',
294
                'GoodsName'             => '',
295
                'Temperature'           => Temperature::ROOM,
296
                'Distance'              => Distance::SAME,
297
                'Specification'         => Specification::CM_60,
298
                'ScheduledPickupTime'   => ScheduledPickupTime::UNLIMITED,
299
                'ScheduledDeliveryTime' => '',
300
                'Remark'                => '',
301
                'PlatformID'            => '',
302
            ];
303
        } elseif (isset($params['LogisticsSubType']) === true) {
304
            // LogisticsSubType::FAMILY = 'FAMI'; // 全家
305
            // LogisticsSubType::UNIMART = 'UNIMART'; // 統一超商
306
            // LogisticsSubType::FAMILY_C2C = 'FAMIC2C'; // 全家店到店
307
            // LogisticsSubType::UNIMART_C2C = 'UNIMARTC2C'; // 統一超商寄貨便
308
309
            // 超商取貨逆物流訂單(全家超商B2C).
310
            $method = 'CreateFamilyB2CReturnOrder';
311
            $supportedParams = [
312
                'AllPayLogisticsID' => '',
313
                'ServerReplyURL'    => '',
314
                'GoodsName'         => '',
315
                'GoodsAmount'       => 0,
316
                'SenderName'        => '',
317
                'SenderPhone'       => '',
318
                'Remark'            => '',
319
                'Quantity'          => '',
320
                'Cost'              => '',
321
                'PlatformID'        => '',
322
            ];
323
        } else {
324
            // 全家逆物流核帳(全家超商B2C).
325
            $method = 'CheckFamilyB2CLogistics';
326
            $supportedParams = [
327
                'RtnMerchantTradeNo' => '',
328
                'PlatformID'         => '',
329
            ];
330
        }
331
332
        $this->sdk->Send = array_merge($this->sdk->Send, $supportedParams);
333
334
        $this->sdk->Send = array_replace(
335
            $this->sdk->Send,
336
            array_intersect_key($params, $this->sdk->Send)
337
        );
338
339
        return call_user_func_array([$this->sdk, $method]);
340
    }
341
342
    /**
343
     * cancelTransaction.
344
     *
345
     * @param array $params
346
     * @return array
347
     */
348
    public function cancelTransaction($params)
349
    {
350
        if (isset($params['LogisticsSubType']) === true) {
351
            // LogisticsSubType::FAMILY = 'FAMI'; // 全家
352
            // LogisticsSubType::UNIMART = 'UNIMART'; // 統一超商
353
            // LogisticsSubType::FAMILY_C2C = 'FAMIC2C'; // 全家店到店
354
            // LogisticsSubType::UNIMART_C2C = 'UNIMARTC2C'; // 統一超商寄貨便
355
356
            // 取消訂單(統一超商C2C).
357
            $method = 'CancelUnimartLogisticsOrder';
358
            $supportedParams = [
359
                'AllPayLogisticsID' => '',
360
                'CVSPaymentNo'      => '',
361
                'CVSValidationNo'   => '',
362
                'PlatformID'        => '',
363
            ];
364
        }
365
366
        $this->sdk->Send = array_merge($this->sdk->Send, $supportedParams);
0 ignored issues
show
Bug introduced by
The variable $supportedParams does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
367
368
        $this->sdk->Send = array_replace(
369
            $this->sdk->Send,
370
            array_intersect_key($params, $this->sdk->Send)
371
        );
372
373
        return call_user_func_array([$this->sdk, $method]);
0 ignored issues
show
Bug introduced by
The variable $method does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
374
    }
375
376
    /**
377
     * getTransactionData.
378
     *
379
     * @param mixed $params
380
     * @return array
381
     */
382
    public function getTransactionData($params)
383
    {
384
        $supportedParams = [
385
            'AllPayLogisticsID' => '',
386
            'PlatformID' => '',
387
        ];
388
389
        $this->sdk->Send = array_merge($this->sdk->Send, $supportedParams);
390
391
        $this->sdk->Send = array_replace(
392
            $this->sdk->Send,
393
            array_intersect_key($params, $this->sdk->Send)
394
        );
395
396
        return $this->sdk->QueryLogisticsInfo();
397
    }
398
}
399