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

File   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 63.64%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 90
ccs 21
cts 33
cp 0.6364
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A moveTo() 0 6 1
A getSize() 0 5 1
A copyTo() 0 5 1
A getContents() 0 5 1
A putContents() 0 9 2
A delete() 0 4 1
A getPath() 0 4 1
A __toString() 0 4 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 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