Passed
Push — master ( 5002ed...772daf )
by Php Easy Api
03:24
created

FileProcess::writeFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Support;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
7
8
class FileProcess
9
{
10
    /**
11
     * @var object
12
     */
13
    public $fs;
14
15
    /**
16
     * @var string
17
     */
18
    public $stubPath;
19
20
    /**
21
     * @var string
22
     */
23
    protected $data;
24
25
    /**
26
     * @var array
27
     */
28
    protected $stubList=array();
29
30
    /**
31
     * FileProcess constructor.
32
     */
33
    public function __construct()
34
    {
35
        $this->fs = new Filesystem();
36
        $this->stubPath = app()->corePath().'/Console/Stubs';
37
    }
38
39
    /**
40
     * @param $file
41
     * @param $data
42
     * @return bool
43
     */
44
    public function dumpFile($file,$data)
45
    {
46
        try {
47
            $this->fs->dumpFile($file,$data);
48
            return true;
49
        } catch (IOExceptionInterface $exception) {
50
            return false;
51
        }
52
    }
53
54
    /**
55
     * @param $data
56
     * @param bool $status
57
     * @return mixed
58
     */
59
    public function makeDirectory($data,$status=false)
60
    {
61
        if($data->type=="project" && file_exists($data->project)){
62
            throw new \LogicException('This Project Is Already Available');
63
        }
64
        if(false===$status){
65
66
            if($data->type!=="project" && !file_exists($data->project)){
67
                throw new \LogicException('Project No');
68
            }
69
        }
70
        foreach ($data->directory as $directory){
71
            try {
72
                $this->fs->mkdir($directory,'0777');
73
                chmod($directory,0777);
74
            } catch (IOExceptionInterface $e) {
75
                return "An error occurred while creating your directory at ".$e->getPath();
76
            }
77
        }
78
    }
79
80
    /**
81
     * @param $directory
82
     * @return mixed
83
     */
84
    public function setDirectory($directory)
85
    {
86
        try {
87
            $this->fs->mkdir($directory,'07777');
88
        } catch (IOExceptionInterface $e) {
89
            return "An error occurred while creating your directory at ".$e->getPath();
90
        }
91
    }
92
93
    /**
94
     * @param $file
95
     */
96
    public function setFile($file)
97
    {
98
        @touch($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for touch(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

98
        /** @scrutinizer ignore-unhandled */ @touch($file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
99
    }
100
101
    /**
102
     * @param $data
103
     * @param array $complex
104
     */
105
    public function touch($data,$complex=array())
106
    {
107
        $this->data=$data;
108
109
        if(isset($complex['stub']) && isset($this->data->argument['stub'])){
110
111
            $this->stubManager($complex);
112
        }
113
114
        $execArray=(count($this->stubList)) ? $this->stubList : $this->data->touch;
115
116
        foreach ($execArray as $execution=>$touch){
117
118
            if(!file_exists($touch) && $touch!==null){
119
                touch($touch);
120
121
                $executionPath=$this->stubPath.'/'.$execution.'.stub';
122
                if(file_exists($executionPath)){
123
                    $this->fopenprocess($executionPath,$touch,$data);
124
                }
125
            }
126
        }
127
    }
128
129
    /**
130
     * @param array $complex
131
     */
132
    private function stubManager($complex=array())
133
    {
134
        $stubStructure      = explode("_",$complex['stub']);
135
        $stubStructure[]    = $this->data->argument['stub'];
0 ignored issues
show
Bug introduced by
The property argument does not exist on string.
Loading history...
136
137
        $stubberDirectoryList=path()->stubs();
138
139
        foreach ($stubStructure as $stubberDirectory){
140
141
            $stubberDirectoryList=$stubberDirectoryList.'/'.$stubberDirectory;
142
143
            $this->setDirectory($stubberDirectoryList);
144
        }
145
146
        foreach ($this->data->touch as $execution=>$executionFile){
0 ignored issues
show
Bug introduced by
The property touch does not exist on string.
Loading history...
147
148
            $executionArray=explode("/",$execution);
149
150
            $executionStub                      = end($executionArray).'';
151
            $this->stubList[$executionStub]     = $executionFile;
152
            $stubberFile                        = $stubberDirectoryList.'/'.$executionStub.'.stub';
153
154
            $originalPath=$this->stubPath.'/'.$execution.'.stub';
155
156
            if(!file_exists($stubberFile)){
157
158
                $this->fs->copy($originalPath,$stubberFile);
159
            }
160
        }
161
162
        $this->stubPath=$stubberDirectoryList;
163
    }
164
165
166
    /**
167
     * @param $executionPath
168
     * @param $path
169
     * @param $param
170
     * @return bool
171
     */
172
    public function fopenprocess($executionPath,$path,$param)
173
    {
174
        $dt = fopen($executionPath, "r");
175
        $content = fread($dt, filesize($executionPath));
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

175
        $content = fread(/** @scrutinizer ignore-type */ $dt, filesize($executionPath));
Loading history...
176
        fclose($dt);
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

176
        fclose(/** @scrutinizer ignore-type */ $dt);
Loading history...
177
178
        foreach ($param->argument as $key=>$value){
179
180
            $content=str_replace("__".$key."__",$value,$content);
181
        }
182
183
        $dt = fopen($path, "w");
184
        fwrite($dt, $content);
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
        fwrite(/** @scrutinizer ignore-type */ $dt, $content);
Loading history...
185
        fclose($dt);
186
187
        return true;
188
    }
189
190
    /**
191
     * @param $executionPath
192
     * @param $path
193
     * @param $param
194
     * @return bool
195
     */
196
    public function stubCopy($executionPath,$path,$param)
197
    {
198
        $dt = fopen($executionPath, "r");
199
        $content = fread($dt, filesize($executionPath));
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
        $content = fread(/** @scrutinizer ignore-type */ $dt, filesize($executionPath));
Loading history...
200
        fclose($dt);
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

200
        fclose(/** @scrutinizer ignore-type */ $dt);
Loading history...
201
202
        foreach ($param->argument as $key=>$value){
203
            $content=str_replace("__".$key."__",$value,$content);
204
        }
205
206
        $dt = fopen($path, "w");
207
        fwrite($dt, $content);
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

207
        fwrite(/** @scrutinizer ignore-type */ $dt, $content);
Loading history...
208
        fclose($dt);
209
210
        return true;
211
    }
212
213
    /**
214
     * @param null $file
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $file is correct as it would always require null to be passed?
Loading history...
215
     * @return mixed|null
216
     */
217
    public function callFile($file=null)
218
    {
219
        if(file_exists($file)){
220
            return require_once($file);
221
        }
222
        return null;
223
    }
224
225
    /**
226
     * write to file for data
227
     *
228
     * @param null|string $file
229
     * @param null|string $data
230
     */
231
    public function writeFile($file=null,$data=null)
232
    {
233
        if(!is_null($file) && !is_null($data)){
234
235
            $dt = fopen($file, "r");
236
            $content = fread($dt, filesize($file));
0 ignored issues
show
Unused Code introduced by
The assignment to $content is dead and can be removed.
Loading history...
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

236
            $content = fread(/** @scrutinizer ignore-type */ $dt, filesize($file));
Loading history...
237
            fclose($dt);
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

237
            fclose(/** @scrutinizer ignore-type */ $dt);
Loading history...
238
239
            $dt = fopen($file, "w");
240
            fwrite($dt, $data);
0 ignored issues
show
Bug introduced by
It seems like $dt can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

240
            fwrite(/** @scrutinizer ignore-type */ $dt, $data);
Loading history...
241
            fclose($dt);
242
        }
243
    }
244
}