Test Failed
Push — master ( de8f0c...763734 )
by Giancarlos
03:36
created

ZipFactory::decompressLastFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

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 3
eloc 10
nc 2
nop 1
crap 3
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 38
    public function compress($filename, $content)
27
    {
28 38
        $archive = new ZipFile();
29 38
        $archive->addFile($content, $filename);
30
31 38
        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
    public function decompress($zipContent, $fileToExtract)
42
    {
43
        $temp = tempnam(sys_get_temp_dir(),time() . '.zip');
44
        file_put_contents($temp, $zipContent);
45
        $zip = new ZipArchive;
46
        $output = "";
47
        if ($zip->open($temp) === true) {
48
            $output = $zip->getFromName($fileToExtract);
49
        }
50
        $zip->close();
51
        unlink($temp);
52
53
        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 16
    public function decompressLastFile($zipContent)
63
    {
64 16
        $temp = tempnam(sys_get_temp_dir(),time() . '.zip');
65 16
        file_put_contents($temp, $zipContent);
66 16
        $zip = new ZipArchive;
67 16
        $output = "";
68 16
        if ($zip->open($temp) === true && $zip->numFiles > 0) {
69 16
            $output = $zip->getFromIndex($zip->numFiles - 1);
70 16
        }
71 16
        $zip->close();
72 16
        unlink($temp);
73
74 16
        return $output;
75
    }
76
}