GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

EnvWriter::save()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 16
cts 16
cp 1
rs 9.1608
c 0
b 0
f 0
cc 5
nc 7
nop 1
crap 5
1
<?php
2
namespace Dbtlr\PHPEnvBuilder;
3
4
use Dbtlr\PHPEnvBuilder\Exception\WritableException;
5
6
class EnvWriter
7
{
8
    /** @var string */
9
    protected $directory;
10
11
    /** @var string */
12
    protected $file;
13
14
    /**
15
     * EnvWriter constructor.
16
     *
17
     * @param string $directory
18
     * @param string $file
19
     */
20 42
    public function __construct($directory, $file = '.env')
21
    {
22 42
        $this->directory = $directory;
23 42
        $this->file = $file;
24 42
    }
25
26
    /**
27
     * Save the given answers to the env file.
28
     *
29
     * @throws WritableException
30
     * @param array $answers
31
     */
32 8
    public function save(array $answers)
33
    {
34 8
        $path = $this->directory . DIRECTORY_SEPARATOR . $this->file;
35
36 8
        if (!file_exists($path)) {
37 6
            if (!is_writable($this->directory)) {
38 4
                throw new WritableException(
39 4
                    sprintf(
40 4
                        'The env file is not present and the directory `%s` is not writeable!',
41 4
                        $this->directory
42
                    )
43
                );
44
            }
45
46 2
            touch($path);
47
        }
48
49 4
        if (!is_writable($path)) {
50 2
            throw new WritableException(sprintf('The env file `%s` is not writeable!', $path));
51
        }
52
53 2
        $text = '';
54 2
        foreach ($answers as $key => $value) {
55 2
            $text .= sprintf("%s=%s\n", $key, $value);
56
        }
57
58 2
        file_put_contents($path, $text);
59 2
    }
60
}
61