FileBackend::loadFromFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/*
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2015 zepi
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 *
25
 */
26
27
/**
28
 * The FileBackend saves and loads the content in and from a file.
29
 * 
30
 * @package Zepi\Turbo\Backend
31
 * @author Matthias Zobrist <[email protected]>
32
 * @copyright Copyright (c) 2015 zepi
33
 */
34
35
namespace Zepi\Turbo\Backend;
36
37
use \Zepi\Turbo\Exception;
38
39
/**
40
 * The FileBackend saves and loads the content in and from a file.
41
 * 
42
 * @author Matthias Zobrist <[email protected]>
43
 * @copyright Copyright (c) 2015 zepi
44
 */
45
class FileBackend
46
{
47
    /**
48
     * @access protected
49
     * @var string
50
     */
51
    protected $path;
52
    
53
    /**
54
     * Constructs the object
55
     * 
56
     * @access public
57
     * @param string $path
58
     */
59 12
    public function __construct($path)
60
    {
61 12
        $this->path = $path;
62 12
    }
63
    
64
    /**
65
     * Saves the content to the file
66
     * 
67
     * @access public
68
     * @param string $content
69
     * @param string $additionalPath
70
     * @return integer
71
     * 
72
     * @throws Zepi\Turbo\Exception The file "$path" isn't writable!
73
     */
74 5
    public function saveToFile($content, $additionalPath = '')
75
    {
76 5
        $path = $this->realPath($additionalPath);
77
        
78
        // If the path does not exists create the directory
79 5
        $this->createTargetDirectory($path);
80
        
81
        // If the file exists but isn't writeable we need to throw an exception
82 4
        if (file_exists($path) && !is_writable($path)) {
83 1
            throw new Exception('The file "' . $path . '" isn\'t writable!');
84
        }
85
        
86 3
        return file_put_contents($path, $content);
87
    }
88
    
89
    /**
90
     * Loads the content from the file
91
     * 
92
     * @access public
93
     * @param string $additionalPath
94
     * @return string
95
     */
96 5
    public function loadFromFile($additionalPath = '')
97
    {
98 5
        $path = $this->testPath($this->realPath($additionalPath));
99
        
100 4
        if ($path === false) {
101 1
            return '';
102
        }
103
        
104 3
        return file_get_contents($path);
105
    }
106
    
107
    /**
108
     * Deletes a file on the hard disk.
109
     * 
110
     * @access public
111
     * @param string $additionalPath
112
     * @return boolean
113
     */
114 4
    public function deleteFile($additionalPath = '')
115
    {
116 4
        $path = $this->testPath($this->realPath($additionalPath));
117
        
118 4
        if ($path === false) {
119 1
            return false;
120
        }
121
        
122 3
        return unlink($path);
123
    }
124
    
125
    /**
126
     * Returns true if a file or a directory exists and
127
     * is writable. Otherwise return false.
128
     * 
129
     * @access public
130
     * @param string $additionalPath
131
     * @return boolean
132
     */
133 4
    public function isWritable($additionalPath = '')
134
    {
135 4
        $path = $this->realPath($additionalPath);
136
        
137 4
        if ($path === false || !is_writable($path)) {
138 1
            return false;
139
        }
140
        
141 3
        return true;
142
    }
143
    
144
    /**
145
     * Returns the real path for the given additional path and the
146
     * file path which was given to the backend in the constructor.
147
     *
148
     * @access public
149
     * @param string $additionalPath
150
     * @return string
151
     */
152 12
    protected function realPath($additionalPath)
153
    {
154 12
        if (substr($additionalPath, 0, 1) === '/') {
155 2
            $path = $additionalPath;
156 10
        } else if ($additionalPath !== '') {
157 1
            $path = $this->path . $additionalPath;
158
        } else {
159 9
            $path = $this->path;
160
        }
161
    
162 12
        return $path;
163
    }
164
    
165
    /**
166
     * If the path exists return the path. Otherwise return false
167
     * or throw an exception.
168
     * 
169
     * @access protected
170
     * @param string $path
171
     * @return false|string
172
     * 
173
     * @throws \Zepi\Turbo\Exception The file path "{path}" is not readable and not writeable!
174
     */
175 7
    protected function testPath($path)
176
    {
177 7
        if (!file_exists($path)) {
178 2
            return false;
179
        }
180
        
181 5
        if (!is_readable($path) && !is_writeable($path)) {
182 1
            throw new Exception('The file path "' . $path . '" is not readable and not writeable!');
183
        }
184
        
185 4
        return $path;
186
    }
187
    
188
    /**
189
     * Creates the target directory
190
     *
191
     * @access protected
192
     * @param string $path
193
     *
194
     * @throws \Zepi\Turbo\Exception The directory "{directory}" doesn't exists
195
     */
196 5
    protected function createTargetDirectory($path)
197
    {
198 5
        $directory = dirname($path);
199 5
        if (!file_exists($directory)) {
200 2
            if (!is_writeable($this->getExistingPath($directory))) {
201 1
                throw new Exception('The directory "' . $directory . '" isn\'t writeable!');
202
            }
203
            
204 1
            mkdir($directory, 0755, true);
205
        }
206 4
    }
207
    
208
    /**
209
     * Returns the existing part of a path
210
     * 
211
     * @access protected
212
     * @param string $path
213
     * @return string
214
     */
215 2
    protected function getExistingPath($path)
216
    {
217 2
        $numberOfSlashes = substr_count($path, '/');
218 2
        $existingPath = $path;
219
        
220 2
        for ($i = 0; $i < $numberOfSlashes; $i++) {
221 2
            if (!file_exists($existingPath)) {
222 2
                $existingPath = dirname($existingPath);
223
            }
224
        }
225
        
226 2
        return $existingPath;
227
    }
228
}
229