FileSystem::writeFileContent()   A
last analyzed

Complexity

Conditions 3
Paths 7

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 3
nc 7
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the LetsEncrypt ACME client.
7
 *
8
 * @author    Ivanov Aleksandr <[email protected]>
9
 * @copyright 2019-2020
10
 * @license   https://github.com/misantron/letsencrypt-client/blob/master/LICENSE MIT License
11
 */
12
13
namespace LetsEncrypt\Helper;
14
15
use LetsEncrypt\Exception\FileIOException;
16
17
final class FileSystem
18
{
19
    /**
20
     * @throws FileIOException
21
     */
22
    public static function readFileContent(string $path): string
23
    {
24
        try {
25
            $file = new \SplFileObject($path, 'r');
26
            $content = $file->fread($file->getSize());
27
            if ($content === false) {
28
                throw new \RuntimeException('content read error');
29
            }
30
31
            return $content;
32
        } catch (\Exception $e) {
33
            throw FileIOException::readError($path, $e);
34
        }
35
    }
36
37
    /**
38
     * @throws FileIOException
39
     */
40
    public static function writeFileContent(string $path, string $content): void
41
    {
42
        try {
43
            $len = strlen($content);
44
45
            $file = new \SplFileObject($path, 'w');
46
            $file->rewind();
47
            if ($len !== $file->fwrite($content)) {
48
                throw new \RuntimeException('content write error');
49
            }
50
            $file->fflush();
51
        } catch (\Exception $e) {
52
            throw FileIOException::writeError($path, $e);
53
        }
54
    }
55
}
56