DefaultEncrypter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 51
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A encrypt() 0 4 1
A decrypt() 0 10 2
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