DefaultEncrypter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CodeZero\Encrypter;
4
5
use Illuminate\Contracts\Encryption\DecryptException as IlluminateDecryptException;
6
use Illuminate\Contracts\Encryption\Encrypter as IlluminateEncrypter;
7
use Illuminate\Encryption\Encrypter as DefaultIlluminateEncrypter;
8
9
class DefaultEncrypter implements Encrypter
10
{
11
    /**
12
     * Laravel's Encrypter
13
     *
14
     * @var IlluminateEncrypter
15
     */
16
    protected $encrypter;
17
18
    /**
19
     * Create a new instance of LaravelEncrypter
20
     *
21
     * @param string $key
22
     * @param IlluminateEncrypter $encrypter
23
     */
24 2
    public function __construct($key, IlluminateEncrypter $encrypter = null)
25
    {
26 2
        $this->encrypter = $encrypter ?: new DefaultIlluminateEncrypter(md5($key), 'AES-256-CBC');
27 2
    }
28
29
    /**
30
     * Encrypt a string
31
     *
32
     * @param string $string
33
     *
34
     * @return string
35
     */
36 1
    public function encrypt($string)
37
    {
38 1
        return $this->encrypter->encrypt($string);
39
    }
40
41
    /**
42
     * Decrypt an encrypted string
43
     *
44
     * @param string $payload
45
     *
46
     * @return string
47
     * @throws DecryptException
48
     */
49 2
    public function decrypt($payload)
50
    {
51
        try {
52 2
            $decrypted = $this->encrypter->decrypt($payload);
53 1
        } catch (IlluminateDecryptException $exception) {
54 1
            throw new DecryptException('Decryption failed.', 0, $exception);
55
        }
56
57 1
        return $decrypted;
58
    }
59
}
60