Completed
Push — master ( f02869...8251a6 )
by Nicolas
03:31
created

EnvFileWriter::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 4
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Modules\Core\Console\Installers\Writers;
2
3
use Dotenv;
4
use Illuminate\Filesystem\Filesystem;
5
6
class EnvFileWriter
7
{
8
    /**
9
     * @var Filesystem
10
     */
11
    private $finder;
12
13
    /**
14
     * @var array
15
     */
16
    protected $search = [
17
        "DB_HOST=localhost",
18
        "DB_DATABASE=homestead",
19
        "DB_USERNAME=homestead",
20
        "DB_PASSWORD=secret",
21
    ];
22
23
    /**
24
     * @var string
25
     */
26
    protected $template = '.env.example';
27
28
    /**
29
     * @var string
30
     */
31
    protected $file = '.env';
32
33
    /**
34
     * @param Filesystem $finder
35
     */
36
    public function __construct(Filesystem $finder)
37
    {
38
        $this->finder = $finder;
39
    }
40
41
    /**
42
     * @param $name
43
     * @param $username
44
     * @param $password
45
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
46
     */
47
    public function write($name, $username, $password, $host)
48
    {
49
        Dotenv::makeMutable();
50
51
        $environmentFile = $this->finder->get($this->template);
52
53
        $replace = [
54
            "DB_HOST=$host",
55
            "DB_DATABASE=$name",
56
            "DB_USERNAME=$username",
57
            "DB_PASSWORD=$password",
58
        ];
59
60
        $newEnvironmentFile = str_replace($this->search, $replace, $environmentFile);
61
62
        $this->finder->put($this->file, $newEnvironmentFile);
63
64
        Dotenv::makeImmutable();
65
    }
66
}
67