FileManager::writeFile()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 3.0175
1
<?php
2
3
namespace Matks\PHPMakeUp\File;
4
5
use Exception;
6
7
/**
8
 * File Manager
9
 */
10
class FileManager implements FileManagerInterface
11
{
12
    /**
13
     * @param string $filepath
14
     * @param array  $lines
15
     */
16
    public function writeFile($filepath, array $lines)
17
    {
18 1
        if (file_exists($filepath)) {
19
            throw new Exception("File $filepath does exist");
20
        }
21
22 1
        $file = fopen($filepath, 'w');
23 1
        foreach ($lines as $line) {
24 1
            fwrite($file, $line);
25 1
        }
26 1
        fclose($file);
27 1
    }
28
29
    /**
30
     * @param string $oldFilepath
31
     * @param string $newFilepath
32
     */
33
    public function replaceFile($oldFilepath, $newFilepath)
34
    {
35 1
        if (!file_exists($oldFilepath)) {
36
            throw new Exception("File $oldFilepath does not exist");
37
        }
38 1
        if (!file_exists($newFilepath)) {
39
            throw new Exception("File $newFilepath does not exist");
40
        }
41
42 1
        unlink($oldFilepath);
43 1
        rename($newFilepath, $oldFilepath);
44 1
    }
45
}
46