Completed
Push — master ( a6e618...41476b )
by Richard
01:12
created

RsaKeyStorageHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 100
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A exists() 0 4 2
A hasPrivateKey() 0 4 1
A hasPublicKey() 0 4 1
A saveKey() 0 5 1
A getPublicKey() 0 8 2
A getPrivateKey() 0 8 2
1
<?php
2
3
4
namespace RichardStyles\EloquentEncryption\FileSystem;
5
6
7
use Illuminate\Support\Facades\Config;
8
use Illuminate\Support\Facades\Storage;
9
use RichardStyles\EloquentEncryption\Contracts\RsaKeyHandler;
10
use RichardStyles\EloquentEncryption\Exceptions\RSAKeyFileMissing;
11
12
class RsaKeyStorageHandler implements RsaKeyHandler
13
{
14
15
    /**
16
     * Storage path for the Public Key File
17
     *
18
     * @var string
19
     */
20
    private $public_key_path;
21
22
    /**
23
     * Storage path for the Private Key File
24
     *
25
     * @var string
26
     */
27
    private $private_key_path;
28
29
    /**
30
     * ApplicationKey constructor.
31
     */
32
    public function __construct()
33
    {
34
        $this->public_key_path =
35
            Config::get('eloquent_encryption.key.public', 'eloquent_encryption.pub');
36
        $this->private_key_path =
37
            Config::get('eloquent_encryption.key.private', 'eloquent_encryption');
38
    }
39
40
    /**
41
     * Have any RSA keys been generated
42
     *
43
     * @return bool
44
     */
45
    public function exists()
46
    {
47
        return $this->hasPrivateKey() && $this->hasPublicKey();
48
    }
49
50
    /**
51
     * A Private key file exists
52
     *
53
     * @return bool
54
     */
55
    public function hasPrivateKey()
56
    {
57
        return Storage::exists($this->private_key_path);
58
    }
59
60
    /**
61
     * A Public key file exists
62
     *
63
     * @return bool
64
     */
65
    public function hasPublicKey()
66
    {
67
        return Storage::exists($this->public_key_path);
68
    }
69
70
    /**
71
     * Save the generated RSA key to the storage location
72
     *
73
     * @param $public
74
     * @param $private
75
     */
76
    public function saveKey($public, $private)
77
    {
78
        Storage::put($this->public_key_path, $public);
79
        Storage::put($this->private_key_path, $private);
80
    }
81
82
    /**
83
     * Get the contents of the public key file
84
     *
85
     * @return string
86
     * @throws RSAKeyFileMissing
87
     */
88
    public function getPublicKey()
89
    {
90
        if (!$this->hasPublicKey()) {
91
            throw new RSAKeyFileMissing();
92
        }
93
94
        return Storage::get($this->public_key_path);
95
    }
96
97
    /**
98
     * Get the contents of the private key file
99
     *
100
     * @return string
101
     * @throws RSAKeyFileMissing
102
     */
103
    public function getPrivateKey()
104
    {
105
        if (!$this->hasPrivateKey()) {
106
            throw new RSAKeyFileMissing();
107
        }
108
109
        return Storage::get($this->private_key_path);
110
    }
111
}
112