Passed
Push — main ( 36f773...e1b064 )
by Sebastian
03:34
created

File::isLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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/captainhook-git/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 string $path;
32
33
    /**
34
     * File constructor.
35
     *
36
     * @param string $path
37
     */
38 90
    public function __construct(string $path)
39
    {
40 90
        $this->path = $path;
41
    }
42
43
    /**
44
     * Path getter.
45
     *
46
     * @return string
47
     */
48 65
    public function getPath(): string
49
    {
50 65
        return $this->path;
51
    }
52
53
    /**
54
     * Checks whether the file exists.
55
     *
56
     * @return bool
57
     */
58 71
    public function exists(): bool
59
    {
60 71
        return is_file($this->path);
61
    }
62
63
    /**
64
     * Reads json file.
65
     *
66
     * @return mixed
67
     * @throws \RuntimeException
68
     */
69 60
    public function read()
70
    {
71 60
        if (!file_exists($this->path)) {
72 1
            throw new RuntimeException('Could not read ' . $this->path);
73
        }
74 59
        return file_get_contents($this->path);
75
    }
76
77
    /**
78
     * Writes file.
79
     *
80
     * @param  string $content
81
     * @throws \RuntimeException
82
     */
83 24
    public function write($content): void
84
    {
85 24
        $this->checkFile();
86 23
        $this->checkDir();
87
88 21
        file_put_contents($this->path, $content);
89
    }
90
91
    /**
92
     * Check if file exists and isn't writable
93
     *
94
     * @return void
95
     * @throws \RuntimeException
96
     */
97 24
    private function checkFile(): void
98
    {
99 24
        if (file_exists($this->path) && !is_writable($this->path)) {
100 1
            throw new RuntimeException('File exists and is not writable');
101
        }
102
    }
103
104
    /**
105
     * Create directory if necessary
106
     *
107
     * @return void
108
     * @throws \RuntimeException
109
     */
110 23
    private function checkDir(): void
111
    {
112 23
        $dir = dirname($this->path);
113 23
        if (!is_dir($dir)) {
114 2
            if (file_exists($dir)) {
115 1
                throw new RuntimeException($dir . ' exists and is not a directory.');
116
            }
117 1
            if (!@mkdir($dir, 0755, true)) {
118 1
                throw new RuntimeException($dir . ' does not exist and could not be created.');
119
            }
120
        }
121
    }
122
123
    /**
124
     * Determines if the given path is a symbolic link
125
     *
126
     * @return bool True if the path is a symbolic link, false otherwise.
127
     */
128 15
    public function isLink(): bool
129
    {
130 15
        return is_link($this->path);
131
    }
132
133
    /**
134
     * Resolves and returns the target of a symbolic link
135
     *
136
     * @return string The resolved target of the symbolic link.
137
     */
138 2
    public function linkTarget(): string
139
    {
140 2
        if (!$this->isLink()) {
141 1
            throw new RuntimeException('Not a symbolic link: ' . $this->path);
142
        }
143 1
        return (string)readlink($this->path);
144
    }
145
}
146