Passed
Pull Request — master (#7)
by Dmitriy
44:11 queued 29:04
created

ContentWriter::write()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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