Credentials   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 5
lcom 2
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 14 2
A store() 0 8 1
A get() 0 4 1
1
<?php
2
3
namespace BeyondCode\Credentials;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Contracts\Encryption\Encrypter;
7
use BeyondCode\Credentials\Exceptions\FileDoesNotExist;
8
9
class Credentials
10
{
11
    const CONFIG_PREFIX = '___credentials_';
12
13
    /**
14
     * The encrypter.
15
     *
16
     * @var \Illuminate\Contracts\Encryption\Encrypter
17
     */
18
    private $encrypter;
19
20
    /**
21
     * The decrypted values array.
22
     *
23
     * @var array
24
     */
25
    private $decrypted;
26
27
    /**
28
     * Create a new Credentials Instance.
29
     *
30
     * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
31
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
32
     */
33
    public function __construct(Encrypter $encrypter)
34
    {
35
        $this->encrypter = $encrypter;
36
    }
37
38
    /**
39
     * Load the file.
40
     *
41
     * @param string $filename
42
     * @return array
43
     */
44
    public function load(string $filename)
45
    {
46
        if (!file_exists($filename)) {
47
            $this->decrypted = [];
48
49
            return $this->decrypted;
50
        }
51
52
        $encrypted = require($filename);
53
54
        $this->decrypted = (array)$this->encrypter->decrypt($encrypted);
55
56
        return $this->decrypted;
57
    }
58
59
    /**
60
     * Store and encrypt the data in the file location.
61
     *
62
     * @param array $data
63
     * @param string $filename
64
     * @return void
65
     */
66
    public function store(array $data, string $filename)
67
    {
68
        $credentials = $this->encrypter->encrypt($data);
69
70
        $encryptedData = '<?php return ' . var_export($credentials, true) . ';';
71
72
        file_put_contents($filename, $encryptedData);
73
    }
74
75
    /**
76
     * Get an encrypter value.
77
     *
78
     * @param string $key
79
     * @param null $default
80
     * @return mixed
81
     */
82
    public function get(string $key, $default = null)
83
    {
84
        return Arr::get($this->decrypted, $key, $default);
85
    }
86
}
87