KmsDriver::encrypt()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
namespace STS\EnvSecurity\Drivers;
4
5
use Aws\Kms\KmsClient;
6
use Illuminate\Contracts\Encryption\Encrypter;
7
8
/**
9
 * Class KmsDriver
10
 * @package STS\EnvSecurity\Drivers
11
 */
12
class KmsDriver implements Encrypter
13
{
14
    /**
15
     * @var KmsClient
16
     */
17
    protected $client;
18
19
    /**
20
     * @var
21
     */
22
    protected $keyId;
23
24
    /**
25
     * KmsDriver constructor.
26
     *
27
     * @param $kmsClient
28
     * @param $keyId
29
     */
30
    public function __construct($kmsClient, $keyId)
31
    {
32
        $this->client = $kmsClient;
33
        $this->keyId = $keyId;
34
    }
35
36
    /**
37
     * @param string $value
38
     * @param bool   $serialize
39
     *
40
     * @return mixed|string
41
     */
42
    public function encrypt($value, $serialize = true)
43
    {
44
        $result = $this->client->encrypt([
45
            'KeyId'     => $this->keyId,
46
            'Plaintext' => $value
47
        ])->get('CiphertextBlob');
48
49
        return ($serialize)
50
            ? base64_encode($result)
51
            : $result;
52
    }
53
54
    /**
55
     * @param string $payload
56
     * @param bool   $unserialize
57
     *
58
     * @return string
59
     */
60
    public function decrypt($payload, $unserialize = true)
61
    {
62
        if ($unserialize) {
63
            $payload = base64_decode($payload);
64
        }
65
66
        return $this->client->decrypt([
67
            'KeyId'          => $this->keyId,
68
            'CiphertextBlob' => $payload
69
        ])->get('Plaintext');
70
    }
71
}