Completed
Push — master ( 891ec6...52a9d3 )
by recca
01:58
created

CreateTransactionAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 59
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 20 4
A locale() 0 14 2
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 1
            throw new HttpRedirect($params['url'].'?locale='.$this->locale(
34 1
                isset($details['locale']) === true ? $details['locale'] : 'zh-TW'
35 1
            ));
36
        }
37
    }
38
39
    /**
40
     * locale.
41
     *
42
     * @param string $locale
43
     * @return string
44
     */
45 1
    protected function locale($locale)
46
    {
47
        $map = [
48 1
            'zh-tw' => 'zh-TW',
49 1
            'tw' => 'zh-TW',
50 1
            'zh-cn' => 'zh-CN',
51 1
            'cn' => 'zh-CN',
52 1
            'en-us' => 'en',
53 1
            'en' => 'en',
54 1
        ];
55 1
        $locale = strtolower($locale);
56
57 1
        return isset($map[$locale]) === true ? $map[$locale] : 'zh-TW';
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 2
    public function supports($request)
64
    {
65
        return
66 2
            $request instanceof CreateTransaction &&
67 2
            $request->getModel() instanceof \ArrayAccess;
68
    }
69
}
70