File   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.86%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 89
ccs 26
cts 28
cp 0.9286
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A getSize() 0 4 1
A write() 0 6 1
A read() 0 4 1
A getPath() 0 4 1
A getName() 0 4 1
A delete() 0 13 3
1
<?php
2
/*
3
 * This file is part of badcow-common.
4
 *
5
 * (c) Samuel Williams <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Badcow\Common;
12
13
use Badcow\Common\File\Mode;
14
15
class File implements FileInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $path;
21
22
    /**
23
     * @var bool
24
     */
25
    private $isDeleted = false;
26
27
    /**
28
     * @param $path
29
     * @throws \ErrorException
30
     */
31 27
    public function __construct($path)
32
    {
33 27
        $this->path = $path;
34
35 27
        if (file_exists($this->path)) {
36 9
            return;
37
        }
38
39 18
        if (!touch($this->path)) {
40
            throw new \ErrorException(sprintf('Cannot create file with path "%s"', $this->path));
41
        }
42 18
    }
43
44
    /**
45
     * @return int
46
     */
47 3
    public function getSize()
48
    {
49 3
        return filesize($this->path);
50
    }
51
52
    /**
53
     * @param string $string
54
     * @param string $mode
55
     */
56 12
    public function write($string, $mode = Mode::OVERWRITE)
57
    {
58 12
        $handle = fopen($this->path, $mode);
59 12
        fwrite($handle, $string);
60 12
        fclose($handle);
61 12
    }
62
63
    /**
64
     * @return string
65
     */
66 3
    public function read()
67
    {
68 3
        return file_get_contents($this->path);
69
    }
70
71
    /**
72
     * @return string
73
     */
74 21
    public function getPath()
75
    {
76 21
        return $this->path;
77
    }
78
79
    /**
80
     * @return string
81
     */
82 3
    public function getName()
83
    {
84 3
        return basename($this->path);
85
    }
86
87
    /**
88
     * Delete the file
89
     */
90 24
    public function delete()
91
    {
92 24
        if ($this->isDeleted) {
93 3
            return;
94
        }
95
96 24
        if (!unlink($this->path)) {
97
            throw new \ErrorException(sprintf('File with path "%s" could not be deleted.', $this->path));
98
        }
99
100 24
        unset($this->path);
101 24
        $this->isDeleted = true;
102
    }
103
}