FileUtils   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 2
b 0
f 0
dl 0
loc 43
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 10 2
A write() 0 10 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 © 2022 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Stdlib;
16
17
/**
18
 * Class FileUtils
19
 *
20
 * @package TxTextControl\ReportingCloud
21
 */
22
class FileUtils extends AbstractStdlib
23
{
24
    /**
25
     * Read a filename from filesystem and return its binary data.
26
     * Optionally, base64 encode the returned binary data.
27
     *
28
     * @param string $filename
29
     * @param bool   $base64Encode
30
     *
31
     * @return string
32
     */
33 58
    public static function read(string $filename, bool $base64Encode = false): string
34
    {
35 58
        $binaryData = file_get_contents($filename);
36 58
        assert(is_string($binaryData));
37
38 58
        if ($base64Encode) {
39 54
            $binaryData = base64_encode($binaryData);
40
        }
41
42 58
        return $binaryData;
43
    }
44
45
    /**
46
     * Write binary data to a filename on filesystem.
47
     * Optionally, based decode the binary data before writing.
48
     *
49
     * @param string $filename
50
     * @param string $binaryData
51
     * @param bool   $base64Encoded
52
     *
53
     * @return bool
54
     */
55 4
    public static function write(string $filename, string $binaryData, bool $base64Encoded = false): bool
56
    {
57 4
        if ($base64Encoded) {
58 2
            $binaryData = base64_decode($binaryData, true);
59 2
            assert(is_string($binaryData));
60
        }
61
62 4
        $result = file_put_contents($filename, $binaryData);
63
64 4
        return is_int($result) && $result > 0;
65
    }
66
}
67