Completed
Push — master ( aa9657...bb9832 )
by Sebastian
05:21
created

File::getPath()   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
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[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
namespace SebastianFeldmann\CaptainHook\Storage;
11
12
/**
13
 * Class File
14
 *
15
 * @package CaptainHook
16
 * @author  Sebastian Feldmann <[email protected]>
17
 * @link    https://github.com/sebastianfeldmann/captainhook
18
 * @since   Class available since Release 0.9.0
19
 */
20
class File
21
{
22
    /**
23
     * Path to file
24
     *
25
     * @var string
26
     */
27
    protected $path;
28
29
    /**
30
     * File constructor.
31
     *
32
     * @param string $path
33
     */
34 30
    public function __construct(string $path)
35
    {
36 30
        $this->path = $path;
37 30
    }
38
39
    /**
40
     * Path getter.
41
     *
42
     * @return string
43
     */
44 2
    public function getPath() : string
45
    {
46 2
        return $this->path;
47
    }
48
49
    /**
50
     * Checks whether the file exists.
51
     *
52
     * @return bool
53
     */
54 19
    public function exists() : bool
55
    {
56 19
        return is_file($this->path);
57
    }
58
59
    /**
60
     * Reads json file.
61
     *
62
     * @throws \RuntimeException
63
     * @return string
64
     */
65 13
    public function read()
66
    {
67 13
        if (!file_exists($this->path)) {
68 1
            throw new \RuntimeException('Could not read ' . $this->path);
69
        }
70 12
        return file_get_contents($this->path);
71
    }
72
73
    /**
74
     * Writes file.
75
     *
76
     * @param  string $content
77
     * @throws \RuntimeException
78
     */
79 10
    public function write($content)
80
    {
81 10
        $this->checkFile();
82 9
        $this->checkDir();
83
84 7
        file_put_contents($this->path, $content);
85 7
    }
86
87
    /**
88
     * Check if file exists and isn't writable
89
     *
90
     * @return void
91
     * @throws \RuntimeException
92
     */
93 10
    private function checkFile()
94
    {
95 10
        if (file_exists($this->path) && !is_writable($this->path)) {
96 1
            throw new \RuntimeException('File exists and is not writable');
97
        }
98 9
    }
99
100
    /**
101
     * Create directory if necessary
102
     *
103
     * @throws \RuntimeException
104
     */
105 9
    private function checkDir()
106
    {
107 9
        $dir = dirname($this->path);
108 9
        if (!is_dir($dir)) {
109 2
            if (file_exists($dir)) {
110 1
                throw new \RuntimeException($dir . ' exists and is not a directory.');
111
            }
112 1
            if (!@mkdir($dir, 0755, true)) {
113 1
                throw new \RuntimeException($dir . ' does not exist and could not be created.');
114
            }
115
        }
116 7
    }
117
}
118