Passed
Pull Request — master (#6)
by Dmitriy
10:40
created

ContentWriter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 1
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B write() 0 11 7
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