AbstractWriter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 45
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getContent() 0 3 1
A __construct() 0 5 1
A getPath() 0 3 1
A save() 0 13 2
A getFilename() 0 3 1
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