Encrypter::encrypt()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 9
nc 2
nop 1
crap 4
1
<?php
2
3
namespace PayumTW\Esunbank;
4
5
class Encrypter
6
{
7
    /**
8
     * $key.
9
     *
10
     * @var string
11
     */
12
    protected $key;
13
14
    /**
15
     * setKey.
16
     *
17
     * @param string $key
18
     */
19 9
    public function setKey($key)
20
    {
21 9
        $this->key = $key;
22
23 9
        return $this;
24
    }
25
26
    /**
27
     * encrypt.
28
     *
29
     * @param array $params
30
     * @return string
31
     */
32 7
    public function encrypt($params = [])
33
    {
34 7
        if (isset($params['MACD']) === true) {
35 5
            foreach (['DATA', 'MACD', 'returnCode', 'version'] as $key) {
36 5
                if (isset($params[$key]) === true) {
37 5
                    unset($params[$key]);
38 5
                }
39 5
            }
40 5
            $string = urldecode(http_build_query($params, '', ',')).',';
41 5
        } else {
42 2
            $string = json_encode($params, JSON_UNESCAPED_SLASHES);
43
        }
44
45 7
        return hash('sha256', $string.$this->key);
46
    }
47
48
    /**
49
     * encryptRequest.
50
     *
51
     * @param array $params
52
     * @param int $ksn
53
     * @return array
54
     */
55 1
    public function encryptRequest($params, $ksn = 1)
56
    {
57
        return [
58 1
            'data' => json_encode($params, JSON_UNESCAPED_SLASHES),
59 1
            'mac' => $this->encrypt($params),
60 1
            'ksn' => $ksn,
61 1
        ];
62
    }
63
}
64