CreateTransactionAction::execute()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4
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
            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
            ));
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 2
    public function supports($request)
43
    {
44
        return
45 2
            $request instanceof CreateTransaction &&
46 2
            $request->getModel() instanceof \ArrayAccess;
47
    }
48
49
    /**
50
     * locale.
51
     *
52
     * @param string $locale
53
     * @return string
54
     */
55 1
    protected function locale($locale)
56
    {
57
        $map = [
58 1
            'zh-tw' => 'zh-TW',
59
            'tw' => 'zh-TW',
60
            'zh-cn' => 'zh-CN',
61
            'cn' => 'zh-CN',
62
            'en-us' => 'en',
63
            'en' => 'en',
64
        ];
65 1
        $locale = strtolower($locale);
66
67 1
        return isset($map[$locale]) === true ? $map[$locale] : 'zh-TW';
68
    }
69
}
70