Passed
Push — master ( 3b7697...25d5dd )
by Alexey
03:48 queued 29s
created

ZipFileData::copyDataToStream()   A

Complexity

Conditions 1
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace PhpZip\Model\Data;
4
5
use PhpZip\Exception\ZipException;
6
use PhpZip\Model\ZipData;
7
8
/**
9
 * Class ZipFileData.
10
 */
11
class ZipFileData implements ZipData
12
{
13
    /** @var \SplFileInfo */
14
    private $file;
15
16
    /**
17
     * ZipStringData constructor.
18
     *
19
     * @param \SplFileInfo $fileInfo
20
     *
21
     * @throws ZipException
22
     */
23
    public function __construct(\SplFileInfo $fileInfo)
24
    {
25
        if (!$fileInfo->isFile()) {
26
            throw new ZipException('$fileInfo is not a file.');
27
        }
28
29
        if (!$fileInfo->isReadable()) {
30
            throw new ZipException('$fileInfo is not readable.');
31
        }
32
33
        $this->file = $fileInfo;
34
    }
35
36
    /**
37
     * @throws ZipException
38
     *
39
     * @return resource returns stream data
40
     */
41
    public function getDataAsStream()
42
    {
43
        if (!$this->file->isReadable()) {
44
            throw new ZipException(sprintf('The %s file is no longer readable.', $this->file->getPathname()));
45
        }
46
47
        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...
48
    }
49
50
    /**
51
     * @throws ZipException
52
     *
53
     * @return string returns data as string
54
     */
55
    public function getDataAsString()
56
    {
57
        if (!$this->file->isReadable()) {
58
            throw new ZipException(sprintf('The %s file is no longer readable.', $this->file->getPathname()));
59
        }
60
61
        return file_get_contents($this->file->getPathname());
62
    }
63
64
    /**
65
     * @param resource $outStream
66
     *
67
     * @throws ZipException
68
     */
69
    public function copyDataToStream($outStream)
70
    {
71
        try {
72
            $stream = $this->getDataAsStream();
73
            stream_copy_to_stream($stream, $outStream);
74
        } finally {
75
            fclose($stream);
76
        }
77
    }
78
}
79