FileWriter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 35
ccs 0
cts 10
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A forceOverwrite() 0 5 1
A write() 0 13 4
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