checkFileLocations()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 1
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\SimpleCounter.
7
 *
8
 * (c) Eric Sizemore <https://github.com/ericsizemore>
9
 *
10
 * This source file is subject to the MIT license. For the full copyright and
11
 * license information, please view the LICENSE file that was distributed with
12
 * this source code.
13
 */
14
/**
15
 * Edit paths below, if needed.
16
 */
17
18
// Update the location to your current log files, if needed.
19
$oldCounterFile = dirname(__DIR__) . '/counter/counter.txt';
20
$oldIpFile      = dirname(__DIR__) . '/counter/ips.txt';
21
22
// Update the location where the new files will be placed, if needed.
23
$newCounterFile = dirname(__DIR__) . '/counter/counter.json';
24
$newIpFile      = dirname(__DIR__) . '/counter/ips.json';
25
26
/**
27
 * NO EDITING BEYOND THIS POINT.
28
 */
29
checkFileLocations($oldCounterFile, $oldIpFile);
30
convertCounterFile($oldCounterFile, $newCounterFile);
31
convertIpFile($oldIpFile, $newIpFile);
32
33
/**
34
 * Helper function to convert count data.
35
 */
36
function convertCounterFile(string $oldCounterFile, string $newCounterFile): void
37
{
38
    echo "Checking counter.txt ...<br/>\n";
39
40
    $countData = trim(file_get_contents($oldCounterFile));
41
42
    if ($countData === '') {
43
        echo "No count data found, creating default data...<br/>\n";
44
        $countData = '0';
45
    } else {
46
        echo "Converting data...<br/>\n";
47
    }
48
49
    $countData = ['currentCount' => $countData];
50
51
    $bytesWritten = file_put_contents($newCounterFile, json_encode($countData), LOCK_EX);
52
53
    if ($bytesWritten === false) {
54
        echo "Unable to update $newCounterFile<br/>\n";
55
    } else {
56
        echo "Count data conversion complete.<br/>\n<br/>\n";
57
    }
58
}
59
60
/**
61
 * Helper function to convert IP data.
62
 */
63
function convertIpFile(string $oldIpFile, string $newIpFile): void
64
{
65
    echo "Checking ips.txt ...<br/>\n";
66
67
    $ipData = trim(file_get_contents($oldIpFile));
68
    $ipData = preg_split("#\n#", $ipData, -1, PREG_SPLIT_NO_EMPTY);
69
70
    if ($ipData === []) {
71
        echo "No IP data found, creating default data...<br/>\n";
72
        $ipData = [''];
73
    } else {
74
        echo "Converting data...<br/>\n";
75
    }
76
77
    $ipData = ['ipList' => $ipData];
78
79
    $bytesWritten = file_put_contents($newIpFile, json_encode($ipData), LOCK_EX);
80
81
    if ($bytesWritten === false) {
82
        echo "Unable to update $newIpFile<br/>\n";
83
    } else {
84
        echo "IP data conversion complete.<br/>\n<br/>\n";
85
    }
86
}
87
88
/**
89
 * Helper function to check file existence.
90
 */
91
function checkFileLocations(string $firstFile, string $secondFile): void
92
{
93
    if (!file_exists($firstFile)) {
94
        throw new RuntimeException(sprintf("'%s' file could not be found.", $firstFile));
95
    }
96
97
    if (!file_exists($secondFile)) {
98
        throw new RuntimeException(sprintf("'%s' file could not be found.", $secondFile));
99
    }
100
}
101