FileWriter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 18
c 1
b 0
f 0
dl 0
loc 38
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A default() 0 5 1
A write() 0 7 2
A pathFor() 0 8 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PhpGenerics\Writer;
4
5
use function dirname;
6
use function file_put_contents;
7
use function is_dir;
8
use function mkdir;
9
use function sprintf;
10
use function str_replace;
11
use const DIRECTORY_SEPARATOR as DS;
12
13
class FileWriter
14
{
15
    /** @var string */
16
    private $baseDirectory;
17
    /** @var string */
18
    private $separator;
19
20
    public function __construct(string $baseDirectory, string $separator)
21
    {
22
        $this->baseDirectory = $baseDirectory;
23
        $this->separator = $separator;
24
    }
25
26
    public static function default(): self
27
    {
28
        return new self(
29
            dirname(__DIR__, 2) . DS . 'Generated' . DS,
30
            DS
31
        );
32
    }
33
34
    public function write(string $namespace, string $class, string $content): void
35
    {
36
        $path = $this->pathFor($namespace, $class);
37
        if (!is_dir(dirname($path))) {
38
            mkdir(dirname($path), 0777, true);
39
        }
40
        file_put_contents($path, $content);
41
    }
42
43
    public function pathFor(string $namespace, string $class): string
44
    {
45
        return sprintf(
46
            '%s%s%s%s.php',
47
            $this->baseDirectory,
48
            str_replace('\\', $this->separator, $namespace),
49
            $this->separator,
50
            $class
51
        );
52
    }
53
}
54