ZipSourceFileData::copyDataToStream()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 5
cp 0.6
crap 2.2559
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace PhpZip\Model\Data;
4
5
use PhpZip\Exception\Crc32Exception;
6
use PhpZip\Exception\ZipException;
7
use PhpZip\IO\ZipReader;
8
use PhpZip\Model\ZipData;
9
use PhpZip\Model\ZipEntry;
10
11
/**
12
 * Class ZipFileData.
13
 */
14
class ZipSourceFileData implements ZipData
15
{
16
    /** @var ZipReader */
17
    private $zipReader;
18
19
    /** @var resource|null */
20
    private $stream;
21
22
    /** @var ZipEntry */
23
    private $sourceEntry;
24
25
    /** @var int */
26
    private $offset;
27
28
    /** @var int */
29
    private $uncompressedSize;
30
31
    /** @var int */
32
    private $compressedSize;
33
34
    /**
35
     * ZipFileData constructor.
36
     *
37
     * @param ZipReader $zipReader
38
     * @param ZipEntry  $zipEntry
39
     * @param int       $offsetData
40
     */
41 134
    public function __construct(ZipReader $zipReader, ZipEntry $zipEntry, $offsetData)
42
    {
43 134
        $this->zipReader = $zipReader;
44 134
        $this->offset = $offsetData;
45 134
        $this->sourceEntry = $zipEntry;
46 134
        $this->compressedSize = $zipEntry->getCompressedSize();
47 134
        $this->uncompressedSize = $zipEntry->getUncompressedSize();
48 134
    }
49
50
    /**
51
     * @param ZipEntry $entry
52
     *
53
     * @return bool
54
     */
55 34
    public function hasRecompressData(ZipEntry $entry)
56
    {
57 34
        return $this->sourceEntry->getCompressionLevel() !== $entry->getCompressionLevel() ||
58 34
            $this->sourceEntry->getCompressionMethod() !== $entry->getCompressionMethod() ||
59 30
            $this->sourceEntry->isEncrypted() !== $entry->isEncrypted() ||
60 30
            $this->sourceEntry->getEncryptionMethod() !== $entry->getEncryptionMethod() ||
61 28
            $this->sourceEntry->getPassword() !== $entry->getPassword() ||
62 28
            $this->sourceEntry->getCompressedSize() !== $entry->getCompressedSize() ||
63 28
            $this->sourceEntry->getUncompressedSize() !== $entry->getUncompressedSize() ||
64 34
            $this->sourceEntry->getCrc() !== $entry->getCrc();
65
    }
66
67
    /**
68
     * @throws ZipException
69
     *
70
     * @return resource returns stream data
71
     */
72 74
    public function getDataAsStream()
73
    {
74 74
        if (!\is_resource($this->stream)) {
75 74
            $this->stream = $this->zipReader->getEntryStream($this);
76
        }
77
78 73
        return $this->stream;
79
    }
80
81
    /**
82
     * @throws ZipException
83
     *
84
     * @return string returns data as string
85
     */
86 70
    public function getDataAsString()
87
    {
88 70
        $autoClosable = $this->stream === null;
89
90 70
        $stream = $this->getDataAsStream();
91 69
        $pos = ftell($stream);
92
93
        try {
94 69
            rewind($stream);
95
96 69
            return stream_get_contents($stream);
97
        } finally {
98 69
            if ($autoClosable) {
99 69
                fclose($stream);
100 69
                $this->stream = null;
101
            } else {
102 69
                fseek($stream, $pos);
103
            }
104
        }
105
    }
106
107
    /**
108
     * @param resource $outputStream Output stream
109
     *
110
     * @throws ZipException
111
     * @throws Crc32Exception
112
     */
113 8
    public function copyDataToStream($outputStream)
114
    {
115 8
        if (\is_resource($this->stream)) {
116
            rewind($this->stream);
117
            stream_copy_to_stream($this->stream, $outputStream);
118
        } else {
119 8
            $this->zipReader->copyUncompressedDataToStream($this, $outputStream);
120
        }
121 7
    }
122
123
    /**
124
     * @param resource $outputStream Output stream
125
     */
126 28
    public function copyCompressedDataToStream($outputStream)
127
    {
128 28
        $this->zipReader->copyCompressedDataToStream($this, $outputStream);
129 28
    }
130
131
    /**
132
     * @return ZipEntry
133
     */
134 79
    public function getSourceEntry()
135
    {
136 79
        return $this->sourceEntry;
137
    }
138
139
    /**
140
     * @return int
141
     */
142 28
    public function getCompressedSize()
143
    {
144 28
        return $this->compressedSize;
145
    }
146
147
    /**
148
     * @return int
149
     */
150 79
    public function getUncompressedSize()
151
    {
152 79
        return $this->uncompressedSize;
153
    }
154
155
    /**
156
     * @return int
157
     */
158 98
    public function getOffset()
159
    {
160 98
        return $this->offset;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 143
    public function __destruct()
167
    {
168 143
        if (\is_resource($this->stream)) {
169 6
            fclose($this->stream);
170
        }
171 143
    }
172
}
173