Passed
Push — master ( 18fc83...7dcdd4 )
by Giancarlos
06:09
created

ZipFactory::decompress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Giansalex
5
 * Date: 16/07/2017
6
 * Time: 12:23
7
 */
8
9
namespace Greenter\Zip;
10
11
use ZipArchive;
12
13
/**
14
 * Class ZipFactory
15
 * @package Greenter\Zip
16
 */
17
final class ZipFactory
18
{
19
    /**
20
     * Comprime el contenido del archivo con el nombre especifico y retorna el contenido del zip.
21
     *
22
     * @param string $filename
23
     * @param string $content
24
     * @return string
25
     */
26 44
    public function compress($filename, $content)
27
    {
28 44
        $archive = new ZipFile();
29 44
        $archive->addFile($content, $filename);
30
31 44
        return $archive->file();
32
    }
33
34
    /**
35
     * Retorna el contenido del archivo especificado dentro del zip.
36
     *
37
     * @param string $zipContent
38
     * @param string $fileToExtract
39
     * @return string
40
     */
41 2
    public function decompress($zipContent, $fileToExtract)
42
    {
43 2
        $temp = tempnam(sys_get_temp_dir(),time() . '.zip');
44 2
        file_put_contents($temp, $zipContent);
45 2
        $zip = new ZipArchive;
46 2
        $output = "";
47 2
        if ($zip->open($temp) === true) {
48 2
            $output = $zip->getFromName($fileToExtract);
49 2
        }
50 2
        $zip->close();
51 2
        unlink($temp);
52
53 2
        return $output;
54
    }
55
56
    /**
57
     * Retorna el contenido del ultimo archivo dentro del zip.
58
     *
59
     * @param string $zipContent
60
     * @return string
61
     */
62 20
    public function decompressLastFile($zipContent)
63
    {
64 20
        $temp = tempnam(sys_get_temp_dir(),time() . '.zip');
65 20
        file_put_contents($temp, $zipContent);
66 20
        $zip = new ZipArchive;
67 20
        $output = "";
68 20
        if ($zip->open($temp) === true && $zip->numFiles > 0) {
69 20
            $output = $zip->getFromIndex($zip->numFiles - 1);
70 20
        }
71 20
        $zip->close();
72 20
        unlink($temp);
73
74 20
        return $output;
75
    }
76
}