File::getSize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace ntentan\utils\filesystem;
4
5
use ntentan\utils\exceptions\FileNotFoundException;
6
use ntentan\utils\exceptions\FileNotReadableException;
7
use ntentan\utils\exceptions\FileNotWriteableException;
8
use ntentan\utils\Filesystem;
9
10
/**
11
 * A file on the filesystem
12
 *
13
 * @author ekow
14
 */
15
class File implements FileInterface
16
{
17
    const OVERWRITE_ALL = 0;
18
    const OVERWRITE_NONE = 1;
19
    const OVERWRITE_OLDER = 2;
20
21
    /**
22
     * Path to file
23
     * @var string
24
     */
25
    protected $path;
26
27
    /**
28
     * File constructor.
29
     *
30
     * @param string $path Path to file. Does
31
     */
32 21
    public function __construct($path)
33
    {
34 21
        $this->path = $path;
35
    }
36
    
37 8
    private function skipOperation($destination, $overwrite)
38
    {
39 8
        return file_exists($destination) && ($overwrite & self::OVERWRITE_NONE || ($overwrite & self::OVERWRITE_OLDER && filemtime($destination) >= filemtime($this->path)));
40
    }
41
42
    /**
43
     * Move file to a new location.
44
     *
45
     * @param string $destination New destination of the file.
46
     * @param int $overwrite Set some overwrite flags on the operation.
47
     * @throws FileNotFoundException
48
     * @throws FileNotWriteableException
49
     */
50 4
    public function moveTo(string $destination, int $overwrite = self::OVERWRITE_ALL) : void
51
    {
52 4
        if($this->skipOperation($destination, $overwrite)) {
53 1
            return;
54
        }
55 3
        $this->copyTo($destination);
56 3
        $this->delete();
57 3
        $this->path = $destination;
58
    }
59
60
    /**
61
     * Get the size of the file.
62
     *
63
     * @return int
64
     * @throws FileNotReadableException
65
     */
66 2
    public function getSize() : int
67
    {
68 2
        Filesystem::checkReadable($this->path);
69 2
        return filesize($this->path);
70
    }
71
72
    /**
73
     * Copy a file to a new destination.
74
     *
75
     * @param string $destination
76
     * @param string $overwrite
77
     * @throws FileNotFoundException
78
     * @throws FileNotWriteableException
79
     */
80 7
    public function copyTo(string $destination, int $overwrite = self::OVERWRITE_ALL) : void
81
    {
82 7
        if($this->skipOperation($destination, $overwrite)) {
83 1
            return;
84
        }
85 6
        $destination = is_dir($destination) ? ("$destination/" . basename($this->path)) : $destination;
86 6
        Filesystem::checkWriteSafety(dirname($destination));
87 6
        copy($this->path, $destination);
88
    }
89
90
    /**
91
     * @return string
92
     * @throws FileNotReadableException
93
     */
94 2
    public function getContents()
95
    {
96 2
        Filesystem::checkReadable($this->path);
97 2
        return file_get_contents($this->path);
98
    }
99
100
    /**
101
     * @param $contents
102
     * @throws FileNotWriteableException
103
     */
104 1
    public function putContents($contents)
105
    {
106 1
        if (file_exists($this->path)) {
107 1
            Filesystem::checkWritable($this->path);
108
        } else {
109 1
            Filesystem::checkWritable(dirname($this->path));
110
        }
111 1
        file_put_contents($this->path, $contents);
112
    }
113
114 5
    public function delete() : void
115
    {
116 5
        unlink($this->path);
117
    }
118
119
    public function deleteIfExists(): void
120
    {
121
        if (file_exists($this->path)) {
122
            $this->delete();
123
        }
124
    }
125
126 1
    public function getPath() : string
127
    {
128 1
        return $this->path;
129
    }
130
131 3
    public function __toString()
132
    {
133 3
        return $this->path;
134
    }
135
136
}
137