Completed
Push — master ( 4051f5...1a3481 )
by recca
02:04
created

CreateTransactionAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 49
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 31 5
A supports() 0 6 2
1
<?php
2
3
namespace PayumTW\Mypay\Action\Api;
4
5
use LogicException;
6
use Payum\Core\Reply\HttpRedirect;
7
use Payum\Core\Bridge\Spl\ArrayObject;
8
use PayumTW\Mypay\Request\Api\CreateTransaction;
9
use Payum\Core\Exception\RequestNotSupportedException;
10
11
class CreateTransactionAction extends BaseApiAwareAction
12
{
13
    /**
14
     * {@inheritdoc}
15
     *
16
     * @param $request CreateTransaction
17
     */
18 2
    public function execute($request)
19
    {
20 2
        RequestNotSupportedException::assertSupports($this, $request);
21
22 2
        $details = ArrayObject::ensureArrayObject($request->getModel());
23
24 2
        $params = $this->api->createTransaction((array) $details);
25
26 2
        $details->replace($params);
27
28 2
        if (isset($params['url']) === false) {
29 1
            throw new LogicException("Response content is not valid json: \n\n".urldecode(json_encode(array_map(function ($data) {
30 1
                return is_string($data) === true ? urlencode($data) : $data;
31 1
            }, $params))));
32
        } else {
33
            $map = [
34 1
                'zh-tw' => 'zh-TW',
35 1
                'tw' => 'zh-TW',
36 1
                'zh-cn' => 'zh-CN',
37 1
                'cn' => 'zh-CN',
38 1
                'en-us' => 'en',
39 1
                'en' => 'en',
40 1
            ];
41 1
            $locale = strtolower(
42 1
                isset($details['locale']) === true ? $details['locale'] : 'zh-TW'
43 1
            );
44 1
            $locale = isset($map[$locale]) === true ? $map[$locale] : 'zh-TW';
45
46 1
            throw new HttpRedirect($params['url'].'?locale='.$locale);
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function supports($request)
54
    {
55
        return
56 2
            $request instanceof CreateTransaction &&
57 2
            $request->getModel() instanceof \ArrayAccess;
58
    }
59
}
60