Completed
Push — dev ( 7d7f55...0c6e86 )
by James Ekow Abaka
01:53
created

File::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ntentan\utils\filesystem;
4
5
use ntentan\utils\Filesystem;
6
7
/**
8
 * Description of File
9
 *
10
 * @author ekow
11
 */
12
class File implements FileInterface
13
{
14
15
    /**
16
     * @var string
17
     */
18
    protected $path;
19
    private $isDirectory;
20
21 1
    public function __construct($path)
22
    {
23 1
        $this->path = $path;
24 1
        if (is_dir($path)) {
25
            $this->isDirectory = true;
26
        }
27 1
    }
28
29
    public function moveTo(string $destination) : void
30
    {
31
        self::copyTo($destination);
32
        unlink($this->path);
33
        $this->path = $destination;
34
    }
35
36
    public function getSize() : integer
37
    {
38
        return filesize($this->path);
39
    }
40
41
    public function copyTo(string $destination) : void
42
    {
43
        Filesystem::checkWriteSafety(dirname($destination));
44
        copy($this->path, $destination);
45
    }
46
47
    public function getContents()
48
    {
49
        Filesystem::checkExists($this->path);
50
        return file_get_contents($this->path);
51
    }
52
53
    public function putContents($contents)
54
    {
55
        if (file_exists($this->path)) {
56
            Filesystem::checkWritable($this->path);
57
        } else {
58
            Filesystem::checkWritable(dirname($this->path));
59
        }
60
        file_put_contents($this->path, $contents);
61
    }
62
63
    public function delete() : void
64
    {
65
        unlink($this->path);
66
    }
67
68
    public function getPath() : string
69
    {
70
        return $this->path;
71
    }
72
73 1
    public function __toString()
74
    {
75 1
        return $this->path;
76
    }
77
78
}
79