Completed
Push — master ( d225ab...33bd8c )
by Lorenzo
02:50
created

IOHelper::makeZip()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 6
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
		{
40
			while (false !== ($entry = readdir($handle)))
41
			{
42
				if($entry != "." && $entry != "..")
43
				{
44
					$zipnome = str_replace(".xml",".zip",$entry);
45
					$zip="";
46
					if(file_exists($dir.$zipnome))
47
					{
48
						$zip = " | <a href=\"".$dir.$zipnome."\">ZIP</a>";
49
					}
50
					if(file_exists($dir.$entry) && strpos($entry,".xml"))
51
					{
52
						$arr[] = "<a target=\"_blank\" href=\"".$dir.$entry."\">".$entry."</a>".$zip."<br>";
53
					}
54
				}
55
			}
56
		closedir($handle);
57
		}
58
		if(is_array($arr))
59
		{
60
			$arr = array_reverse($arr);
61
			foreach ($arr as $a)
62
			{
63
				echo $a;
64
			}
65
		}
66
	}
67
68
	/**
69
	 * @param string $dir
70
	 */
71
	public static function cleandir($dir = "output")
72
	{
73
		if(empty($dir)){
74
			$dir = "output";
75
		}
76
77
		$files = glob($dir."/*");
78
		if(is_array($files))
79
			{
80
			foreach($files as $file)
81
				{
82
					if(is_file($file))
83
					{
84
						unlink($file);
85
					}
86
				}
87
			}
88
	}
89
90
	/**
91
	 * @param $str
92
	 * @param $pathOutput
93
	 * @param $basePath
94
	 */
95
	public static function outputFile($str, $pathOutput, $basePath)
96
	{
97
		$filesystem = new Filesystem(new AdapterLocal($basePath));
98
		$filesystem->put(basename($pathOutput), $str);
99
	}
100
101
	/**
102
	 * @param $source
103
	 * @param $destination
104
	 */
105
	public static function zipFile($source, $destination)
106
	{
107
		$filesystem = new Filesystem(new AdapterZip($destination));
108
		$filesystem->put(basename($source), file_get_contents($source));
109
        self::makeZip($filesystem);
110
	}
111
112
	/**
113
	 * @param $source
114
	 * @param $destination
115
	 * @param $str
116
	 */
117
	public static function zipFileOntheFly($source, $destination, $str)
118
	{
119
		$filesystem = new Filesystem(new AdapterZip($destination));
120
		$filesystem->put(basename($source), $str);
121
        self::makeZip($filesystem);
122
	}
123
124
    /**
125
     * @param Filesystem $filesystem
126
     */
127
    public static function makeZip(Filesystem $filesystem)
128
    {
129
        $zip = $filesystem->getAdapter();
130
        if (is_a($zip, 'ZipArchiveAdapter')) {
131
            $zip->getArchive()->close();
132
        }
133
    }
134
}
135