IOHelper::zipFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
namespace Padosoft\TesseraSanitaria;
3
4
use League\Flysystem\Filesystem;
5
use League\Flysystem\ZipArchive\ZipArchiveAdapter as AdapterZip;
6
//local file
7
use League\Flysystem\Adapter\Local as AdapterLocal;
8
9
/**
10
 * Class IOHelper
11
 * @package Padosoft\TesseraSanitaria
12
 */
13
class IOHelper
14
{
15
    /**
16
     *
17
     */
18
    public static function printDir()
19
    {
20
        echo '<br /><br />
21
		<div class="page-header"><h1>Elenco File Generati:</h1></div>
22
		';
23
24
        self::getDir();
25
    }
26
27
    /**
28
     * @param string $dir
29
     */
30
    public static function getDir($dir = "output/")
31
    {
32
        if (empty($dir)) {
33
            $dir = "output";
34
        }
35
36
        $arr = array();
37
38
        if ($handle = opendir($dir)) {
39
            while (false !== ($entry = readdir($handle))) {
40
                if ($entry != "." && $entry != "..") {
41
                    $zipnome = str_replace(".xml", ".zip", $entry);
42
                    $zip = "";
43
                    if (file_exists($dir . $zipnome)) {
44
                        $zip = " | <a href=\"" . $dir . $zipnome . "\">ZIP</a>";
45
                    }
46
                    if (file_exists($dir . $entry) && strpos($entry, ".xml")) {
47
                        $arr[] = "<a target=\"_blank\" href=\"" . $dir . $entry . "\">" . $entry . "</a>" . $zip . "<br>";
48
                    }
49
                }
50
            }
51
            closedir($handle);
52
        }
53
        if (is_array($arr)) {
54
            $arr = array_reverse($arr);
55
            foreach ($arr as $a) {
56
                echo $a;
57
            }
58
        }
59
    }
60
61
    /**
62
     * @param string $dir
63
     */
64
    public static function cleandir($dir = "output")
65
    {
66
        if (empty($dir)) {
67
            $dir = "output";
68
        }
69
70
        $files = glob($dir . "/*");
71
        if (is_array($files)) {
72
            foreach ($files as $file) {
73
                if (is_file($file)) {
74
                    unlink($file);
75
                }
76
            }
77
        }
78
    }
79
80
    /**
81
     * @param $str
82
     * @param $pathOutput
83
     * @param $basePath
84
     */
85
    public static function outputFile($str, $pathOutput, $basePath)
86
    {
87
        $filesystem = new Filesystem(new AdapterLocal($basePath));
88
        $filesystem->put(basename($pathOutput), $str);
89
    }
90
91
    /**
92
     * @param $source
93
     * @param $destination
94
     */
95
    public static function zipFile($source, $destination)
96
    {
97
        $filesystem = new Filesystem(new AdapterZip($destination));
98
        $filesystem->put(basename($source), file_get_contents($source));
99
        self::makeZip($filesystem);
100
    }
101
102
    /**
103
     * @param $source
104
     * @param $destination
105
     * @param $str
106
     */
107
    public static function zipFileOntheFly($source, $destination, $str)
108
    {
109
        $filesystem = new Filesystem(new AdapterZip($destination));
110
        $filesystem->put(basename($source), $str);
111
        self::makeZip($filesystem);
112
    }
113
114
    /**
115
     * @param Filesystem $filesystem
116
     */
117
    public static function makeZip(Filesystem $filesystem)
118
    {
119
        $zip = $filesystem->getAdapter();
120
        if (is_a($zip, 'ZipArchiveAdapter')) {
121
            $zip->getArchive()->close();
122
        }
123
    }
124
}
125