FileWriter::forceOverwrite()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Generator;
10
11
final class FileWriter
12
{
13
    private bool $overwrite = false;
14
15
    /**
16
     * Forces to overwrite file, but only in next write attempt
17
     *
18
     * @return $this
19
     */
20
    public function forceOverwrite(): self
21
    {
22
        $this->overwrite = true;
23
24
        return $this;
25
    }
26
27
    /**
28
     * Writes given content into specified file path
29
     *
30
     * @param string $path
31
     * @param string $content
32
     */
33
    public function write(string $path, string $content): void
34
    {
35
        $dir = dirname($path);
36
37
        if (false === is_dir($dir)) {
38
            mkdir($dir, 0777, true);
39
        }
40
41
        if (true === $this->overwrite || false === file_exists($path)) {
42
            file_put_contents($path, $content);
43
        }
44
45
        $this->overwrite = false;
46
    }
47
}
48