FileWriter::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Xervice\DataProvider\Business\Model\Generator;
6
7
8
use Xervice\DataProvider\Business\Exception\GenerateDirectoryNotWriteable;
9
10
class FileWriter implements FileWriterInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $generatedPath;
16
17
    /**
18
     * FileWriter constructor.
19
     *
20
     * @param string $path
21
     *
22
     * @throws \Xervice\DataProvider\Business\Exception\GenerateDirectoryNotWriteable
23
     */
24 10
    public function __construct(string $path)
25
    {
26 10
        $this->generatedPath = $path;
27
        if (
28 10
            !is_writable($this->generatedPath)
29 10
            && !mkdir($this->generatedPath, 0777, true)
30 10
            && !is_dir($this->generatedPath)
31
        ) {
32
            throw new GenerateDirectoryNotWriteable($this->generatedPath);
33
        }
34 10
    }
35
36
    /**
37
     * @param string $filename
38
     * @param string $content
39
     */
40 10
    public function writeToFile(string $filename, string $content): void
41
    {
42 10
        file_put_contents(
43 10
            $this->generatedPath . '/' . $filename,
44 10
            sprintf(
45 10
                '<?php%1$sdeclare(strict_types=1);%1$s%2$s',
46 10
                PHP_EOL,
47 10
                $content
48
            )
49
        );
50 10
    }
51
52
53
}
54