Completed
Push — master ( 69dd33...0cd5d9 )
by Pierre
01:46
created

FileManager::copyFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace PierInfor\GeoLite;
4
5
use PierInfor\GeoLite\Downloader;
6
7
/**
8
 * FileManager class to manage files
9
 */
10
class FileManager implements Interfaces\FileManagerInterface
11
{
12
13
    /**
14
     * Downloader instance
15
     *
16
     * @var Downloader
17
     */
18
    private $downloader;
19
20
    /**
21
     * Instanciate
22
     */
23
    public function __construct()
24
    {
25
        $this->downloader = new Downloader();
26
    }
27
28
    /**
29
     * returns downloader instance
30
     *
31
     * @return Downloader
32
     */
33
    public function getDownloader(): Downloader
34
    {
35
        return $this->downloader;
36
    }
37
38
    /**
39
     * download a file for a given url and filename
40
     *
41
     * @param string $url
42
     * @param string $toFilename
43
     * @return FileManager
44
     */
45
    public function download(string $url, string $toFilename): FileManager
46
    {
47
        $this->downloader->download($url, $toFilename);
48
        return $this;
49
    }
50
51
    /**
52
     * uncompress a tar gz file to a tar archive and return true if ok
53
     *
54
     * @param string $tgzFilename
55
     * @return boolean
56
     * @throws \Exception
57
     */
58
    public function ungz(string $tgzFilename): bool
59
    {
60
        $result = false;
61
        if (empty($tgzFilename) || !file_exists($tgzFilename)) {
62
            return $result;
63
        }
64
        try {
65
            (new \PharData($tgzFilename))->decompress();
66
        } catch (\Exception $e) {
67
            if ($e instanceof \UnexpectedValueException) {
68
                throw new \Exception('Malformed gzip');
69
            }
70
        }
71
        return true;
72
    }
73
74
    /**
75
     * extract a tar archive to a target folder
76
     *
77
     * @param string $tarFilename
78
     * @param string $targetFolder
79
     * @return boolean
80
     * @return void
81
     *
82
     */
83
    public function untar(string $tarFilename, string $targetFolder): bool
84
    {
85
        if (empty($tarFilename)
86
            || empty($targetFolder)
87
            || !file_exists($tarFilename)
88
        ) {
89
            return false;
90
        }
91
        (new \PharData($tarFilename))->extractTo($targetFolder);
92
        return true;
93
    }
94
95
    /**
96
     * copy file from to and returns operation status
97
     *
98
     * @param string $from
99
     * @param string $to
100
     * @return boolean
101
     */
102
    public function copyFile(string $from, string $to): bool
103
    {
104
        return @copy($from, $to);
105
    }
106
107
    /**
108
     * returns folders list for a given path
109
     *
110
     * @param string $path
111
     * @return array
112
     */
113
    public function folderList(string $path): array
114
    {
115
        return @glob($path . '*', GLOB_ONLYDIR);
0 ignored issues
show
Bug Best Practice introduced by
The expression return @glob($path . '*'...r\GeoLite\GLOB_ONLYDIR) could return the type false which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
116
    }
117
118
    /**
119
     * returns file list for a given path
120
     *
121
     * @param string $path
122
     * @return array
123
     */
124
    public function fileList(string $path): array
125
    {
126
        return @glob(rtrim($path, '/') . '/*.{*}', GLOB_BRACE);
0 ignored issues
show
Bug Best Practice introduced by
The expression return @glob(rtrim($path...for\GeoLite\GLOB_BRACE) could return the type false which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
127
    }
128
129
    /**
130
     * returns Y-m-d modification file date for a $path
131
     *
132
     * @param string $path
133
     * @return string
134
     */
135
    public function fileDate(string $path): string
136
    {
137
        clearstatcache();
138
        if (!file_exists($path)) {
139
            return '1971-01-14';
140
        }
141
        return date('Y-m-d', filemtime($path));
142
    }
143
144
    /**
145
     * returns true if modified file date is today
146
     *
147
     * @param string $path
148
     * @return string
149
     */
150
    public function isFileDateToday(string $path): bool
151
    {
152
        return (date('Y-m-d') == $this->fileDate($path));
0 ignored issues
show
Bug Best Practice introduced by
The expression return date('Y-m-d') == $this->fileDate($path) returns the type boolean which is incompatible with the documented return type string.
Loading history...
153
    }
154
155
    /**
156
     * unlink multiples files from a path and a mask
157
     *
158
     * @param string $mask
159
     * @return void
160
     */
161
    public function unlinkFiles(string $mask)
162
    {
163
        $toDelete = glob($mask);
164
        $toDeleteCount = count($toDelete);
0 ignored issues
show
Bug introduced by
It seems like $toDelete can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, 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

164
        $toDeleteCount = count(/** @scrutinizer ignore-type */ $toDelete);
Loading history...
165
        for ($c = 0; $c < $toDeleteCount; $c++) {
166
            @unlink($toDelete[$c]);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). 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

166
            /** @scrutinizer ignore-unhandled */ @unlink($toDelete[$c]);

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...
167
        }
168
    }
169
170
    /**
171
     * unlink multiples folders even if not empty from a path
172
     *
173
     * @param string $mask
174
     * @return void
175
     */
176
    public function unlinkFolders(string $path)
177
    {
178
        $toDelete = $this->folderList($path);
179
        $toDeleteCount = count($toDelete);
180
        for ($c = 0; $c < $toDeleteCount; $c++) {
181
            $this->deleteFolder($toDelete[$c]);
182
        }
183
    }
184
185
    /**
186
     * delete a not empty folder
187
     *
188
     * @param string $path
189
     * @return boolean
190
     */
191
    public function deleteFolder(string $path): bool
192
    {
193
        $fsItNoDots = \FilesystemIterator::SKIP_DOTS;
194
        $rdiT = new \RecursiveDirectoryIterator($path, $fsItNoDots);
195
        $riiFirstChild = \RecursiveIteratorIterator::CHILD_FIRST;
196
        $rii = new \RecursiveIteratorIterator($rdiT, $riiFirstChild);
197
        foreach ($rii as $file) {
198
            if ($file->isDir()) {
199
                @rmdir($file->getPathname());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). 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

199
                /** @scrutinizer ignore-unhandled */ @rmdir($file->getPathname());

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...
200
            } else {
201
                @unlink($file->getPathname());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). 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

201
                /** @scrutinizer ignore-unhandled */ @unlink($file->getPathname());

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...
202
            }
203
        }
204
        return @rmdir($path);
205
    }
206
}
207