File::fileExists()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
/**
3
 * Wrapper for php file commmands
4
 *
5
 * @package     erdiko/core
6
 * @copyright   2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com
7
 * @author      Varun Brahme
8
 * @author      Coleman Tung
9
 * @author      John Arroyo <[email protected]>
10
 */
11
namespace erdiko\core\datasource;
12
13
14
class File
15
{
16
    /**
17
     * Default Path
18
     */
19
    protected $_filePath = null;
20
    
21
    /**
22
    * Contructor
23
    *
24
    * @param string $defaultPath
25
    */
26
    public function __construct($defaultPath = null)
27
    {
28
        if (isset($defaultPath)) {
29
            $this->_filePath=$defaultPath;
30
        } else {
31
            $rootFolder=dirname(dirname(dirname(__DIR__)));
32
            $this->_filePath=$rootFolder."/var";
33
        }
34
        if (!is_dir($this->_filePath)) {
35
            mkdir($this->_filePath, 0775, true);
36
        }
37
    }
38
39
    /**
40
     * Create Directory
41
     * If path doesn't exist, create it
42
     */
43
    private function createDir($path)
44
    {
45
        if (!is_dir($path)) {
46
            $success = mkdir($path, 0775, true);
47
            if (!$success) {
48
                throw new \Exception("Cannot create folder {$path}. Check file system permissions.");
49
            }
50
        }
51
    }
52
    
53
    /**
54
     * Write string to file
55
     *
56
     * @param string $content
57
     * @param string $filename
58
     * @param string $pathToFile
59
     * @param string $mode - Default mode: w
60
     * @return int - bytes written to file
61
     */
62
    public function write($content, $filename, $pathToFile = null, $mode = "w")
63
    {
64
        if ($pathToFile == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $pathToFile of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
65
            $pathToFile = $this->_filePath;
66
        }
67
68
        $this->createDir($pathToFile);
69
70
        $fileHandle = fopen($pathToFile."/".$filename, $mode);
71
        $success = fwrite($fileHandle, $content);
72
        fclose($fileHandle);
73
74
        return $success;
75
    }
76
    
77
    /**
78
     * Read string to file
79
     *
80
     * @param string $filename
81
     * @param string $pathToFile
82
     * @return string
83
     */
84 View Code Duplication
    public function read($filename, $pathToFile = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        if(!$this->fileExists($filename, $pathToFile))
87
            throw new \Exception("File, '{$filename}', does not exist.");
88
89
        if ($pathToFile==null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $pathToFile of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
90
            return file_get_contents($this->_filePath."/".$filename);
91
        } else {
92
            return file_get_contents($pathToFile."/".$filename);
93
        }
94
    }
95
    
96
    /**
97
     * Delete a file
98
     *
99
     * @param string $filename
100
     * @param string $pathToFile
101
     * @return bool
102
     */
103 View Code Duplication
    public function delete($filename, $pathToFile = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        if ($pathToFile==null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $pathToFile of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
106
            $pathToFile=$this->_filePath;
107
        }
108
        if (file_exists($pathToFile."/".$filename)) {
109
            return unlink($pathToFile."/".$filename);
110
        } else {
111
            return false;
112
        }
113
    }
114
115
    /**
116
     * Move a file
117
     *
118
     * @param string $filename
119
     * @param string $pathTo
120
     * @param string $pathToFrom
0 ignored issues
show
Documentation introduced by
There is no parameter named $pathToFrom. Did you maybe mean $pathTo?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
121
     * @return bool
122
     */
123 View Code Duplication
    public function move($filename, $pathTo, $pathFrom = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        if ($pathFrom==null) {
126
            $pathFrom=$this->_filePath;
127
        }
128
        if (file_exists($pathFrom."/".$filename)) {
129
            $this->createDir($pathTo);
130
            return rename($pathFrom."/".$filename, $pathTo."/".$filename);
131
        } else {
132
            return null;
133
        }
134
    }
135
    
136
    /**
137
     * Rename a file
138
     *
139
     * @param string $oldName
140
     * @param string $pathTo
0 ignored issues
show
Documentation introduced by
There is no parameter named $pathTo. Did you maybe mean $pathToFile?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
141
     * @param string $pathToFrom
0 ignored issues
show
Bug introduced by
There is no parameter named $pathToFrom. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
142
     * @return bool
143
     * @todo consider merging rename() and move() into one method
144
     */
145 View Code Duplication
    public function rename($oldName, $newName, $pathToFile = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
    {
147
        if ($pathToFile==null) {
148
            $pathToFile=$this->_filePath;
149
        }
150
        if (file_exists($pathToFile."/".$oldName)) {
151
            $this->createDir($pathToFile);
152
            return rename($pathToFile."/".$oldName, $pathToFile."/".$newName);
153
        } else {
154
            return false;
155
        }
156
    }
157
    
158
    /**
159
     * Copy a file
160
     *
161
     * @param string $filename
162
     * @param string $newFilePath
163
     * @param string $newFileName
164
     * @param string $pathToFile
165
     * @return bool
166
     */
167
    public function copy($filename, $newFilePath, $newFileName = null, $pathToFile = null)
168
    {
169
        if ($pathToFile==null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $pathToFile of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
170
            $pathToFile=$this->_filePath;
171
        }
172
        if ($newFileName==null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $newFileName of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
173
            $newFileName=$filename;
174
        }
175
        if (file_exists($pathToFile."/".$filename)) {
176
            return copy($pathToFile."/".$filename, $newFilePath."/".$newFileName);
177
        } else {
178
            return false;
179
        }
180
    }
181
    
182
    /**
183
     * Check if a file exists
184
     *
185
     * @param string $filename
186
     * @param string $pathToFile
187
     * @return bool
188
     */
189
    public function fileExists($filename, $pathToFile = null)
190
    {
191
        if ($pathToFile==null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $pathToFile of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
192
            $pathToFile=$this->_filePath;
193
        }
194
        return file_exists($pathToFile."/".$filename);
195
    }
196
    
197
    /**
198
     * Destructor
199
     */
200
    public function __destruct()
201
    {
202
    }
203
}
204