FileWriter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 38
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 4
A writeToFile() 0 8 1
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