FileGenerator::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace generators;
4
5
use generators\AbstractGenerator;
6
use filesystem\Filesystem;
7
8
class FileGenerator extends \generators\AbstractFileGenerator
9
{
10
    private $generator;
11
    private $templateFilename;
12
    private $filename;
13
    private $filesystem;
14
15
    function __construct(
16
        AbstractGenerator $generator,
17
        Filesystem $filesystem
18
    )
19
    {
20
        $this->generator        = $generator;
21
        $this->filename         = $this->generator->getPath();
22
        $this->templateFilename = $this->generator->getTemplateFilename();
23
        $this->filesystem       = $filesystem;
24
    }
25
26
    public function readFromTemplate(): FileGenerator
27
    {
28
        if (!$this->filesystem->exists($this->templateFilename)) {
29
            throw new \InvalidArgumentException('File not found: ' . $this->templateFilename);
30
        }
31
        
32
        $this->generator->setContent(
33
            $this->filesystem->read($this->templateFilename)
34
        );
35
36
        return $this;
37
    }
38
39
    public function read(): FileGenerator
40
    {
41
        $this->generator->setContent(
42
            $this->filesystem->read($this->filename)
43
        );
44
45
        return $this;
46
    }
47
48
    public function write(): FileGenerator
49
    {
50
        $this->filesystem->write(
51
            $this->filename,
52
            $this->generator->toString()
53
        );
54
55
        return $this;
56
    }
57
58
    public function exists(): bool
59
    {
60
        return $this->filesystem->exists($this->filename);
61
    }
62
63
    public function remove(): FileGenerator
64
    {
65
        $this->filesystem->delete($this->filename);
66
67
        return $this;
68
    }
69
70
    public function throwIfExists(string $message = ''): FileGenerator
71
    {
72
        if ($this->exists()) {
73
            throw new \UnexpectedValueException($message);
74
        }
75
76
        return $this;
77
    }
78
79
    public function throwIfNotExists(string $message = ''): FileGenerator
80
    {
81
        if (!$this->exists()) {
82
            throw new \UnexpectedValueException($message);
83
        }
84
85
        return $this;
86
    }
87
88
    public function extract(): AbstractGenerator
89
    {
90
        return $this->generator;
91
    }
92
}
93