Encrypter::filter()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 12
1
<?php
2
3
namespace PayumTW\Collect;
4
5
class Encrypter
6
{
7
    /**
8
     * $key.
9
     *
10
     * @var string
11
     */
12
    public $key;
13
14
    /**
15
     * setKey.
16
     *
17
     * @param string $key
18
     */
19
    public function setKey($key)
20
    {
21
        $this->key = $key;
22
23
        return $this;
24
    }
25
26
    /**
27
     * encrypt.
28
     *
29
     * @param array $params
30
     * @param array $filterKeys
31
     * @return string
32
     */
33
    public function encrypt($params, $filterKeys = [])
34
    {
35
        if (empty($filterKeys) === false) {
36
            $params = $this->filter($params, $filterKeys);
37
        }
38
39
        return isset($params['status']) === true
40
            ? $this->hash($params, ':')
41
            : $this->hash(array_merge([$this->key], $params), '$');
42
    }
43
44
    /**
45
     * filter.
46
     * @param array $params
47
     * @param array $filterKeys
48
     * @return array
49
     */
50
    protected function filter($params, $filterKeys)
51
    {
52
        $results = [];
53
        foreach ($filterKeys as $key) {
54
            if (isset($params[$key]) === true) {
55
                $results[$key] = $params[$key];
56
            }
57
        }
58
59
        return $results;
60
    }
61
62
    /**
63
     * hash.
64
     *
65
     * @param array $params
66
     * @param string $separate
67
     * @return string
68
     */
69
    protected function hash($params, $separate = '$')
70
    {
71
        return hash('md5', implode($separate, $params));
72
    }
73
}
74