Completed
Push — master ( 0433ad...53e08c )
by Jared
02:26
created

EncryptCache::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 09/08/2017
6
 * Time: 16:22
7
 */
8
9
namespace JC;
10
11
/**
12
 * Class EncryptCache
13
 * @package JC
14
 */
15
class EncryptCache extends SimpleCache
16
{
17
    /**
18
     *
19
     */
20
    const ENCRYPT_METHOD = 'AES256';
21
22
    /**
23
     * @var string
24
     */
25
    public static $encryptKey;
26
27
    /**
28
     * @return string
29
     */
30
    public static function getEncryptKey()
31
    {
32
        return self::$encryptKey;
33
    }
34
35
    /**
36
     * @param string $encryptKey
37
     */
38
    public static function setEncryptKey($encryptKey)
39
    {
40
        self::$encryptKey = $encryptKey;
41
    }
42
43
    /**
44
     * @param object $object
45
     * @return string
46
     */
47
    protected static function encode($object)
48
    {
49
        return openssl_encrypt(json_encode($object), self::ENCRYPT_METHOD, self::getEncryptKey(), 0, self::getIv());
50
    }
51
52
    /**
53
     * @param string $string
54
     * @return object
55
     */
56
    protected static function decode($string)
57
    {
58
        return json_decode(openssl_decrypt($string, self::ENCRYPT_METHOD, self::getEncryptKey(), 0, self::getIv()));
59
    }
60
61
    /**
62
     * @return bool|string
63
     */
64
    protected static function getIv()
65
    {
66
        return substr(md5(self::getEncryptKey()), 0, 16);
67
    }
68
69
}