Crypter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 61
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A encrypt() 0 4 1
A decrypt() 0 4 1
A validateKeyAndIv() 0 9 3
1
<?php
2
namespace Tzsk\Crypt;
3
4
use Tzsk\Crypt\Exceptions\IvLengthException;
5
use Tzsk\Crypt\Exceptions\KeyLengthException;
6
7
class Crypter
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $key;
13
14
    /**
15
     * @var string
16
     */
17
    protected $iv;
18
19
    /**
20
     * @var string
21
     */
22
    protected $cipher = 'AES-256-CBC';
23
24
    /**
25
     * Crypter constructor.
26
     *
27
     */
28
    public function __construct()
29
    {
30
        $this->key = config('crypt.key');
31
        $this->iv = config('crypt.iv');
32
33
        $this->validateKeyAndIv();
34
    }
35
36
    /**
37
     * @param $data
38
     * @return string
39
     */
40
    public function encrypt($data)
41
    {
42
        return openssl_encrypt($data, $this->cipher, $this->key, 0, $this->iv);
43
    }
44
45
    /**
46
     * @param $data
47
     * @return string
48
     */
49
    public function decrypt($data)
50
    {
51
        return openssl_decrypt($data, $this->cipher, $this->key, 0, $this->iv);
52
    }
53
54
    /**
55
     * @throws IvLengthException
56
     * @throws KeyLengthException
57
     */
58
    protected function validateKeyAndIv()
59
    {
60
        if (strlen($this->key) != 32) {
61
            throw new KeyLengthException();
62
        }
63
        if (strlen($this->iv) != 16) {
64
            throw new IvLengthException();
65
        }
66
    }
67
}
68