Passed
Push — master ( e0a48b...50471a )
by Jonathan
16:31
created

FileUtils::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 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 79
    public static function read(string $filename, bool $base64Encode = false): string
34
    {
35 79
        $binaryData = (string) file_get_contents($filename);
36
37 79
        if ($base64Encode) {
38 73
            $binaryData = (string) base64_encode($binaryData);
39
        }
40
41 79
        return $binaryData;
42
    }
43
44
    /**
45
     * Write binary data to a filename on filesystem.
46
     * Optionally, based decode the binary data before writing.
47
     *
48
     * @param string $filename
49
     * @param string $binaryData
50
     * @param bool   $base64Encoded
51
     *
52
     * @return bool
53
     */
54 6
    public static function write(string $filename, string $binaryData, bool $base64Encoded = false): bool
55
    {
56 6
        if ($base64Encoded) {
57 3
            $binaryData = (string) base64_decode($binaryData);
58
        }
59
60 6
        $result = file_put_contents($filename, $binaryData);
61
62 6
        if (is_int($result) && $result > 0) {
63 6
            $ret = true;
64
        } else {
65
            $ret = false;
66
        }
67
68 6
        return $ret;
69
    }
70
}
71