EnvironmentManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Helpers\Installer;
4
5
use Exception;
6
use Illuminate\Http\Request;
7
8
class EnvironmentManager
9
{
10
    /**
11
     * @var string
12
     */
13
    private $envPath;
14
15
    /**
16
     * @var string
17
     */
18
    private $envExamplePath;
19
20
    /**
21
     * Set the .env and .env.example paths.
22
     */
23
    public function __construct()
24
    {
25
        $this->envPath = base_path('.env');
26
        $this->envExamplePath = base_path('.env.example');
27
    }
28
29
    /**
30
     * Get the content of the .env file.
31
     *
32
     * @return string
33
     */
34
    public function getEnvContent($request)
35
    {
36
        $data = $request->session()->all();
37
38
        $env = '';
39
        foreach($data as $key => $value) {
40
            if(preg_match('/^[A-Z]+_[A-Z]+$/', $key)) {
41
                $env .= "${key}=${value}" . PHP_EOL;
42
            }
43
        }
44
45
        return $env;
46
    }
47
48
    /**
49
     * Save the edited content to the file.
50
     *
51
     * @param Request $request
52
     * @return boolean
53
     */
54
    public function saveFile(Request $request)
55
    {
56
        $message = trans('installer.environment.success');
57
        $status = true;
58
59
        try {
60
            file_put_contents($this->envPath, $request->get('envConfig'));
61
        } catch(Exception $e) {
62
            $status = false;
63
        }
64
65
        $request->session()->flash('status', $message);
66
67
        return $status;
68
    }
69
}
70