Passed
Branch 4.9 (cb955a)
by Mikhail
01:30
created

Filesystem::rename()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
c 0
b 0
f 0
rs 9.9
cc 4
nc 4
nop 2
1
<?php
2
3
namespace filesystem;
4
5
final class Filesystem
6
{
7
    public function exists(string $raw_filename)
8
    {
9
        $filename = get_absolute_path($raw_filename);
10
11
        return file_exists($filename);
12
    }
13
14
    public function read(string $raw_filename)
15
    {
16
        $filename = get_absolute_path($raw_filename);
17
18
        return @file_get_contents($filename);
19
    }
20
21
    public function delete(string $filename): bool
22
    {
23
        $filename_clean = get_absolute_path($filename);
24
25
        if (is_dir($filename_clean)) {
26
            $it = new \RecursiveDirectoryIterator($filename_clean, \RecursiveDirectoryIterator::SKIP_DOTS);
27
            $files = new \RecursiveIteratorIterator($it,
28
                        \RecursiveIteratorIterator::CHILD_FIRST);
29
            foreach($files as $file) {
30
                if ($file->isDir()){
31
                    rmdir($file->getRealPath());
32
                } else {
33
                    unlink($file->getRealPath());
34
                }
35
            }
36
            return rmdir($filename_clean);
37
        } else {
38
            return unlink($filename_clean);
39
        }
40
    }
41
42
    public function write(string $raw_filename, string $data)
43
    {
44
        $filename = get_absolute_path($raw_filename);
45
        $pathinfo = pathinfo($filename);
46
        if (!file_exists($pathinfo['dirname'])) {
47
            mkdir($pathinfo['dirname'], 0777, true);
48
        }
49
50
        return false !== file_put_contents($filename, $data);
51
    }
52
53
    /**
54
     * @param string $raw_filename - full filename to be renamed
55
     * @param string $new_name - new name of the file, relative to $filename path
56
     * 
57
     * @return bool
58
     */
59
    public function rename(string $raw_filename, string $new_name)
60
    {
61
        $filename = get_absolute_path($raw_filename);
62
        if (!$this->exists($filename)) {
63
            throw new \InvalidArgumentException("File $filename doesn't exists");
64
        }
65
        
66
        $pathinfo = pathinfo($filename);
67
        $new_filename = get_absolute_path($pathinfo['dirname'] . '/' . $new_name);
68
69
        if ($this->exists($new_filename)) {
70
            throw new \InvalidArgumentException("File $new_filename already exists");
71
        }
72
    
73
        if (rename($filename, $new_filename)) {
74
            return $new_filename;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $new_filename returns the type string which is incompatible with the documented return type boolean.
Loading history...
75
        } else {
76
            return false;
77
        }
78
    }
79
}
80