Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
16 / 16
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractAdapter
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
16 / 16
21
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isLocal
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLocation
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLocation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fetchFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 uploadFile
n/a
0 / 0
n/a
0 / 0
0
 uploadFileStream
n/a
0 / 0
n/a
0 / 0
0
 replaceFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deleteFile
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 rmdir
n/a
0 / 0
n/a
0 / 0
0
 mkdir
n/a
0 / 0
n/a
0 / 0
0
 copyFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 renameFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fileExists
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 isFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileMTime
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 md5File
n/a
0 / 0
n/a
0 / 0
0
 loadFile
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 checkFileLocation
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Storage;
15
16use Pop\Storage\Exception;
17use Pop\Http\Server\Upload;
18
19/**
20 * Abstract storage adapter class
21 *
22 * @category   Pop
23 * @package    Pop\Storage
24 * @author     Nick Sagona, III <dev@nolainteractive.com>
25 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
26 * @license    http://www.popphp.org/license     New BSD License
27 * @version    1.0.0
28 */
29abstract class AbstractAdapter implements AdapterInterface
30{
31
32    /**
33     * Is local flag
34     * @var boolean
35     */
36    protected $local = false;
37
38    /**
39     * Storage location
40     * @var string
41     */
42    protected $location = null;
43
44    /**
45     * Constructor
46     *
47     * @param string $location
48     */
49    public function __construct($location)
50    {
51        $this->setLocation($location);
52    }
53
54    /**
55     * Is storage local
56     *
57     * @return boolean
58     */
59    public function isLocal()
60    {
61        return $this->local;
62    }
63
64    /**
65     * Set storage location
66     *
67     * @param  string $location
68     * @return AbstractAdapter
69     */
70    public function setLocation($location)
71    {
72        $this->location = $location;
73        return $this;
74    }
75
76    /**
77     * Get storage location
78     *
79     * @return string
80     */
81    public function getLocation()
82    {
83        return $this->location;
84    }
85
86    /**
87     * Fetch file
88     *
89     * @param  string $filename
90     * @return string
91     */
92    public function fetchFile($filename)
93    {
94        return file_get_contents($this->checkFileLocation($filename));
95    }
96
97    /**
98     * Upload file
99     *
100     * @param  mixed   $file
101     * @param  string  $dest
102     * @param  Upload  $upload
103     * @return string
104     */
105    abstract public function uploadFile($file, $dest = null, Upload $upload = null);
106
107    /**
108     * Upload file stream
109     *
110     * @param  string  $fileStream
111     * @param  string  $filename
112     * @param  string  $folder
113     * @return string
114     */
115    abstract public function uploadFileStream($fileStream, $filename, $folder = null);
116
117    /**
118     * Replace file
119     *
120     * @param  string $filename
121     * @param  string $contents
122     * @return void
123     */
124    public function replaceFile($filename, $contents)
125    {
126        file_put_contents($this->checkFileLocation($filename), $contents);
127    }
128
129    /**
130     * Delete
131     *
132     * @param  string $filename
133     * @return void
134     */
135    public function deleteFile($filename)
136    {
137        if ($this->fileExists($filename)) {
138            unlink($this->checkFileLocation($filename));
139        }
140    }
141
142    /**
143     * Remove a directory
144     *
145     * @param  string $dir
146     * @return void
147     */
148    abstract public function rmdir($dir);
149
150    /**
151     * Make a directory
152     *
153     * @param  string $dir
154     * @return void
155     */
156    abstract public function mkdir($dir);
157
158    /**
159     * Copy file
160     *
161     * @param  string $filename
162     * @param  string $to
163     * @return void
164     */
165    public function copyFile($filename, $to)
166    {
167        copy($this->checkFileLocation($filename), $this->location . $to);
168    }
169
170    /**
171     * Rename file
172     *
173     * @param  string $filename
174     * @param  string $to
175     * @return void
176     */
177    public function renameFile($filename, $to)
178    {
179        rename($this->checkFileLocation($filename), $this->location . $to);
180    }
181
182    /**
183     * File exists
184     *
185     * @param  string $filename
186     * @return boolean
187     */
188    public function fileExists($filename)
189    {
190        if (!file_exists($filename)) {
191            $filename = $this->location . $filename;
192        }
193        return file_exists($filename);
194    }
195
196    /**
197     * Check if file is a file
198     *
199     * @param  string $filename
200     * @return boolean
201     */
202    public function isFile($filename)
203    {
204        return is_file($this->checkFileLocation($filename));
205    }
206
207    /**
208     * Get file size
209     *
210     * @param  string $filename
211     * @return int
212     */
213    public function getFileSize($filename)
214    {
215        return filesize($this->checkFileLocation($filename));
216    }
217
218    /**
219     * Get file type
220     *
221     * @param  string $filename
222     * @return string
223     */
224    public function getFileType($filename)
225    {
226        return filetype($this->checkFileLocation($filename));
227    }
228
229    /**
230     * Get file modified time
231     *
232     * @param  string $filename
233     * @return int
234     */
235    public function getFileMTime($filename)
236    {
237        return filemtime($this->checkFileLocation($filename));
238    }
239
240    /**
241     * Create MD5 checksum of the file
242     *
243     * @param  string $filename
244     * @return string
245     */
246    abstract public function md5File($filename);
247
248    /**
249     * Load file lines into array
250     *
251     * @param  string $filename
252     * @return array
253     */
254    public function loadFile($filename)
255    {
256        if (!file_exists($filename)) {
257            $filename = $this->location . $filename;
258        }
259        return file($this->checkFileLocation($filename));
260    }
261
262    /**
263     * Load file lines into array
264     *
265     * @param  string $filename
266     * @throws Exception
267     * @return string
268     */
269    protected function checkFileLocation($filename)
270    {
271        if (!file_exists($filename)) {
272            $filename = $this->location . $filename;
273        }
274        if (!file_exists($filename)) {
275            throw new Exception("Error: The file '" . $filename . "' does not exist.");
276        }
277        return $filename;
278    }
279
280}