Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

File::checkDir()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

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