Passed
Branch master (d23cf5)
by Jonathan
22:25 queued 01:36
created

FileUtils::write()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 4
nop 3
crap 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP SDK
6
 *
7
 * PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://git.io/Jejj2 for the canonical source repository
11
 * @license   https://git.io/Jejjr
12
 * @copyright © 2023 Text Control GmbH
13
 */
14
15
namespace TextControl\ReportingCloud\Stdlib;
16
17
class FileUtils extends AbstractStdlib
18
{
19
    /**
20
     * Read a filename from filesystem and return its binary data.
21
     * Optionally, base64 encode the returned binary data.
22
     */
23 58
    public static function read(string $filename, bool $base64Encode = false): string
24
    {
25 58
        $binaryData = file_get_contents($filename);
26 58
        assert(is_string($binaryData));
27
28 58
        if ($base64Encode) {
29 54
            $binaryData = base64_encode($binaryData);
30
        }
31
32 58
        return $binaryData;
33
    }
34
35
    /**
36
     * Write binary data to a filename on filesystem.
37
     * Optionally, based decode the binary data before writing.
38
     */
39 4
    public static function write(string $filename, string $binaryData, bool $base64Encoded = false): bool
40
    {
41 4
        if ($base64Encoded) {
42 2
            $binaryData = base64_decode($binaryData, true);
43 2
            assert(is_string($binaryData));
44
        }
45
46 4
        $result = file_put_contents($filename, $binaryData);
47
48 4
        return is_int($result) && 0 < $result;
49
    }
50
}
51