HandlesEnvFiles   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 54
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadEncrypted() 0 9 3
A saveDecrypted() 0 7 2
A saveEncrypted() 0 5 1
A getFilePathForEnvironment() 0 3 1
1
<?php
2
3
namespace STS\EnvSecurity\Console\Concerns;
4
5
/**
6
 * Class HandlesEnvFiles
7
 * @package STS\EnvSecurity\Console\Concerns
8
 */
9
trait HandlesEnvFiles
10
{
11
    /**
12
     * @param $environment
13
     *
14
     * @return bool|null|string
15
     */
16
    protected function loadEncrypted($environment)
17
    {
18
        $path = $this->getFilePathForEnvironment($environment);
19
20
        if (file_exists($path) && is_readable($path)) {
21
            return file_get_contents($path);
22
        }
23
24
        return null;
25
    }
26
27
    /**
28
     * @param $ciphertext
29
     * @param $environment
30
     *
31
     * @return bool
32
     */
33
    protected function saveEncrypted($ciphertext, $environment)
34
    {
35
        $path = $this->getFilePathForEnvironment($environment);
36
37
        return file_put_contents($path, $ciphertext) !== false;
38
    }
39
40
    /**
41
     * @param      $plaintext
42
     * @param null $output
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $output is correct as it would always require null to be passed?
Loading history...
43
     *
44
     * @return bool
45
     */
46
    protected function saveDecrypted($plaintext, $output = null)
47
    {
48
        if (!$output) {
0 ignored issues
show
introduced by
$output is of type null, thus it always evaluated to false.
Loading history...
49
            $output = config('env-security.destination');
50
        }
51
52
        return file_put_contents($output, $plaintext) !== false;
53
    }
54
55
    /**
56
     * @param $environment
57
     *
58
     * @return string
59
     */
60
    protected function getFilePathForEnvironment($environment)
61
    {
62
        return config('env-security.store') . '/' . $environment . '.env.enc';
63
    }
64
}