Completed
Push — master ( 9b124d...317457 )
by Jasper
03:01
created

BinaryFileResponse::setFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 4
dl 0
loc 16
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Swis\Laravel\Encrypted;
4
5
use Symfony\Component\HttpFoundation\BinaryFileResponse as BaseBinaryFileResponse;
6
7
class BinaryFileResponse extends BaseBinaryFileResponse
8
{
9
    /**
10
     * @var \Swis\Laravel\Encrypted\File
11
     */
12
    protected $file;
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 15
    public function setFile(
18
        $file,
19
        string $contentDisposition = null,
20
        bool $autoEtag = false,
21
        bool $autoLastModified = true
22
    ) {
23
        // Wrap the file in our own \Swis\Laravel\Encrypted\File class.
24 15
        if (!$file instanceof File) {
25 15
            if ($file instanceof \SplFileInfo) {
26 3
                $file = new File($file->getPathname());
27
            } else {
28 12
                $file = new File((string) $file);
29
            }
30
        }
31
32 15
        return parent::setFile($file, $contentDisposition, $autoEtag, $autoLastModified);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 6
    public function sendContent()
39
    {
40 6
        if (!$this->isSuccessful()) {
41
            return parent::sendContent();
42
        }
43
44 6
        if (0 === $this->maxlen) {
45
            return $this;
46
        }
47
48 6
        $out = fopen('php://output', 'wb');
49
50
        // Create a temporary file stream with the decrypted contents.
51 6
        $file = tmpfile();
52 6
        fwrite($file, $this->file->getContents());
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        fwrite(/** @scrutinizer ignore-type */ $file, $this->file->getContents());
Loading history...
53 6
        fseek($file, 0);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        fseek(/** @scrutinizer ignore-type */ $file, 0);
Loading history...
54
55 6
        stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $source of stream_copy_to_stream() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        stream_copy_to_stream(/** @scrutinizer ignore-type */ $file, $out, $this->maxlen, $this->offset);
Loading history...
Bug introduced by
It seems like $out can also be of type false; however, parameter $dest of stream_copy_to_stream() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        stream_copy_to_stream($file, /** @scrutinizer ignore-type */ $out, $this->maxlen, $this->offset);
Loading history...
56
57 6
        fclose($out);
0 ignored issues
show
Bug introduced by
It seems like $out can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        fclose(/** @scrutinizer ignore-type */ $out);
Loading history...
58 6
        fclose($file);
59
60 6
        if ($this->deleteFileAfterSend && file_exists($this->file->getPathname())) {
61 3
            unlink($this->file->getPathname());
62
        }
63
64 6
        return $this;
65
    }
66
}
67