Issues (54)

src/Model/Data/ZipFileData.php (1 issue)

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 49
    public function __construct(ZipEntry $zipEntry, \SplFileInfo $fileInfo)
26
    {
27 49
        if (!$fileInfo->isFile()) {
28
            throw new ZipException('$fileInfo is not a file.');
29
        }
30
31 49
        if (!$fileInfo->isReadable()) {
32
            throw new ZipException('$fileInfo is not readable.');
33
        }
34
35 49
        $this->file = $fileInfo;
36 49
        $zipEntry->setUncompressedSize($fileInfo->getSize());
37 49
    }
38
39
    /**
40
     * @throws ZipException
41
     *
42
     * @return resource returns stream data
43
     */
44 50
    public function getDataAsStream()
45
    {
46 50
        if (!$this->file->isReadable()) {
47
            throw new ZipException(sprintf('The %s file is no longer readable.', $this->file->getPathname()));
48
        }
49
50 50
        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