AbstractWriter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Generator\Domain\Writers;
11
12
abstract class AbstractWriter implements WriterInterface
13
{
14
    private string $content;
15
16
    private string $filename;
17
18
    private string $path;
19
20
    public function __construct(string $content, string $filename, string $path)
21
    {
22
        $this->content = $content;
23
        $this->filename = $filename;
24
        $this->path = $path;
25
    }
26
27
    public function save(): string
28
    {
29
        $path = $this->getPath();
30
31
        if (!\is_dir($path)) {
32
            \mkdir($path, 0777, true); // @codeCoverageIgnore
33
        }
34
35
        $output = \sprintf('%1$s/%2$s.%3$s', $path, $this->getFilename(), $this->getExtension());
36
37
        \file_put_contents($output, $this->getContent());
38
39
        return $output;
40
    }
41
42
    abstract protected function getExtension(): string;
43
44
    private function getContent(): string
45
    {
46
        return $this->content;
47
    }
48
49
    private function getFilename(): string
50
    {
51
        return $this->filename;
52
    }
53
54
    private function getPath(): string
55
    {
56
        return $this->path;
57
    }
58
}
59