Completed
Push — dev ( 0c6e86...8d8070 )
by James Ekow Abaka
01:21
created

File::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.032
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 5
    public function __construct($path)
22
    {
23 5
        $this->path = $path;
24 5
        if (is_dir($path)) {
25
            $this->isDirectory = true;
26
        }
27 5
    }
28
29
    /**
30
     * @param string $destination
31
     * @throws \ntentan\utils\exceptions\FileNotFoundException
32
     * @throws \ntentan\utils\exceptions\FileNotWriteableException
33
     */
34 1
    public function moveTo(string $destination) : void
35
    {
36 1
        $this->copyTo($destination);
37 1
        $this->delete();
38 1
        $this->path = $destination;
39 1
    }
40
41
    /**
42
     * @return int
43
     * @throws \ntentan\utils\exceptions\FileNotReadableException
44
     */
45 1
    public function getSize() : int
46
    {
47 1
        Filesystem::checkReadable($this->path);
48 1
        return filesize($this->path);
49
    }
50
51
    /**
52
     * @param string $destination
53
     * @throws \ntentan\utils\exceptions\FileNotFoundException
54
     * @throws \ntentan\utils\exceptions\FileNotWriteableException
55
     */
56 2
    public function copyTo(string $destination) : void
57
    {
58 2
        Filesystem::checkWriteSafety(dirname($destination));
59 2
        copy($this->path, $destination);
60 2
    }
61
62
    /**
63
     * @return string
64
     * @throws \ntentan\utils\exceptions\FileNotReadableException
65
     */
66
    public function getContents()
67
    {
68
        Filesystem::checkReadable($this->path);
69
        return file_get_contents($this->path);
70
    }
71
72
    /**
73
     * @param $contents
74
     * @throws \ntentan\utils\exceptions\FileNotWriteableException
75
     */
76
    public function putContents($contents)
77
    {
78
        if (file_exists($this->path)) {
79
            Filesystem::checkWritable($this->path);
80
        } else {
81
            Filesystem::checkWritable(dirname($this->path));
82
        }
83
        file_put_contents($this->path, $contents);
84
    }
85
86 2
    public function delete() : void
87
    {
88 2
        unlink($this->path);
89 2
    }
90
91
    public function getPath() : string
92
    {
93
        return $this->path;
94
    }
95
96 3
    public function __toString()
97
    {
98 3
        return $this->path;
99
    }
100
101
}
102