Passed
Push — master ( d305ab...074443 )
by Alexey
03:50 queued 12s
created

ZipFileData::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 10
1
<?php
2
3
namespace PhpZip\Model\Data;
4
5
use PhpZip\Exception\ZipException;
6
use PhpZip\Model\ZipData;
7
use PhpZip\Model\ZipEntry;
8
9
/**
10
 * Class ZipFileData.
11
 */
12
class ZipFileData implements ZipData
13
{
14
    /** @var \SplFileInfo */
15
    private $file;
16
17
    /**
18
     * ZipStringData constructor.
19
     *
20
     * @param ZipEntry     $zipEntry
21
     * @param \SplFileInfo $fileInfo
22
     *
23
     * @throws ZipException
24
     */
25 35
    public function __construct(ZipEntry $zipEntry, \SplFileInfo $fileInfo)
26
    {
27 35
        if (!$fileInfo->isFile()) {
28
            throw new ZipException('$fileInfo is not a file.');
29
        }
30
31 35
        if (!$fileInfo->isReadable()) {
32
            throw new ZipException('$fileInfo is not readable.');
33
        }
34
35 35
        $this->file = $fileInfo;
36 35
        $zipEntry->setUncompressedSize($fileInfo->getSize());
37 35
    }
38
39
    /**
40
     * @throws ZipException
41
     *
42
     * @return resource returns stream data
43
     */
44 35
    public function getDataAsStream()
45
    {
46 35
        if (!$this->file->isReadable()) {
47
            throw new ZipException(sprintf('The %s file is no longer readable.', $this->file->getPathname()));
48
        }
49
50 35
        return fopen($this->file->getPathname(), 'rb');
0 ignored issues
show
Bug Best Practice introduced by
The expression return fopen($this->file->getPathname(), 'rb') could also return false which is incompatible with the documented return type resource. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
51
    }
52
53
    /**
54
     * @throws ZipException
55
     *
56
     * @return string returns data as string
57
     */
58
    public function getDataAsString()
59
    {
60
        if (!$this->file->isReadable()) {
61
            throw new ZipException(sprintf('The %s file is no longer readable.', $this->file->getPathname()));
62
        }
63
64
        return file_get_contents($this->file->getPathname());
65
    }
66
67
    /**
68
     * @param resource $outStream
69
     *
70
     * @throws ZipException
71
     */
72
    public function copyDataToStream($outStream)
73
    {
74
        try {
75
            $stream = $this->getDataAsStream();
76
            stream_copy_to_stream($stream, $outStream);
77
        } finally {
78
            fclose($stream);
79
        }
80
    }
81
}
82