Passed
Push — main ( 4122b8...60aebc )
by Miaad
01:36
created

file::delete()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 19
rs 9.2222
cc 6
nc 6
nop 2
1
<?php
2
3
namespace BPT\tools;
4
5
use BPT\constants\loggerTypes;
6
use BPT\exception\bptException;
7
use BPT\logger;
8
use FilesystemIterator;
9
use RecursiveDirectoryIterator;
10
use RecursiveIteratorIterator;
11
use BPT\tools;
12
use ZipArchive;
13
14
trait file {
15
    /**
16
     * receive size from path(can be url or file path)
17
     *
18
     * NOTE : some url will not return real size!
19
     *
20
     * e.g. => tools::size('xFile.zip');
21
     *
22
     * e.g. => tools::size(path: 'xFile.zip');
23
     *
24
     * @param string $path   file path, could be url
25
     * @param bool   $format if you set this true , you will receive symbolic string like 2.76MB for return
26
     *
27
     * @return string|int|false string for formatted data , int for normal data , false when size can not be found(file not found or ...)
28
     */
29
    public static function size (string $path, bool $format = true): string|int|false {
30
        if (filter_var($path, FILTER_VALIDATE_URL)) {
31
            $ch = curl_init($path);
32
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
33
            curl_setopt($ch, CURLOPT_HEADER, true);
34
            curl_setopt($ch, CURLOPT_NOBODY, true);
35
            curl_exec($ch);
36
            $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
37
            curl_close($ch);
38
        }
39
        else {
40
            $size = file_exists($path) ? filesize($path) : false;
41
        }
42
        if (isset($size) && is_numeric($size)) {
43
            return $format ? tools::byteFormat($size) : $size;
44
        }
45
        else return false;
46
    }
47
48
    /**
49
     * Delete a folder or file if exist
50
     *
51
     * e.g. => tools::delete(path: 'xfolder/yfolder');
52
     *
53
     * e.g. => tools::delete('xfolder/yfolder',false);
54
     *
55
     * @param string $path folder or file path
56
     * @param bool   $sub  set true for removing subFiles too, if folder has subFiles and this set to false , you will receive error
57
     *
58
     * @return bool
59
     * @throws bptException
60
     */
61
    public static function delete (string $path, bool $sub = true): bool {
62
        if (is_dir($path)) {
63
            if (count(scandir($path)) > 2) {
64
                if ($sub) {
65
                    $it = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
66
                    $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
67
                    foreach ($files as $file) {
68
                        $file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath());
69
                    }
70
                    return rmdir($path);
71
                }
72
                else {
73
                    logger::write("tools::delete function used\ndelete function cannot delete folder because its have subFiles and sub parameter haven't true value",loggerTypes::ERROR);
74
                    throw new bptException('DELETE_FOLDER_HAS_SUB');
75
                }
76
            }
77
            else return rmdir($path);
78
        }
79
        else return unlink($path);
80
    }
81
82
    /**
83
     * convert all files in selected path to zip and then save it in dest path
84
     *
85
     * e.g. => tools::zip('xFolder','yFolder/xFile.zip');
86
     *
87
     * @param string $path        your file or folder to be zipped
88
     * @param string $destination destination path for create file
89
     *
90
     * @return bool
91
     * @throws bptException when zip extension not found
92
     */
93
    public static function zip (string $path, string $destination): bool {
94
        if (extension_loaded('zip')) {
95
            $rootPath = realpath($path);
96
            $zip = new ZipArchive();
97
            $zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE);
98
            if (is_dir($path)) {
99
                $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
100
                $root_len = strlen($rootPath) + 1;
101
                foreach ($files as $file) {
102
                    if (!$file->isDir()) {
103
                        $filePath = $file->getRealPath();
104
                        $zip->addFile($filePath, substr($filePath, $root_len));
105
                    }
106
                }
107
            }
108
            else {
109
                $zip->addFile($path, basename($path));
110
            }
111
            return $zip->close();
112
        }
113
        else {
114
            logger::write("tools::zip function used\nzip extension is not found , It may not be installed or enabled", loggerTypes::ERROR);
115
            throw new bptException('ZIP_EXTENSION_MISSING');
116
        }
117
    }
118
}