Passed
Push — master ( f5390f...ff2936 )
by Alexander
02:09
created

ContentWriter::write()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 9.5839

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
ccs 5
cts 8
cp 0.625
rs 8.8333
cc 7
nc 4
nop 2
crap 9.5839
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Composer\Config;
6
7
use Yiisoft\Composer\Config\Exception\FailedWriteException;
8
9
class ContentWriter
10
{
11 1
    public function write(string $path, string $content): void
12
    {
13 1
        if (file_exists($path) && $content === file_get_contents($path)) {
14
            return;
15
        }
16 1
        $dirname = dirname($path);
17 1
        if (!file_exists($dirname) && !mkdir($dirname, 0777, true) && !is_dir($dirname)) {
18
            throw new FailedWriteException(sprintf('Directory "%s" was not created', $dirname));
19
        }
20 1
        if (false === file_put_contents($path, $content)) {
21
            throw new FailedWriteException("Failed write file $path");
22
        }
23 1
    }
24
}
25