Completed
Push — master ( 48985c...642fe5 )
by Jasper
12:59 queued 09:23
created

BinaryFileResponse::sendContent()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 27
ccs 13
cts 15
cp 0.8667
rs 9.4888
cc 5
nc 4
nop 0
crap 5.0592
1
<?php
2
3
namespace Swis\Filesystem\Encrypted;
4
5
use Symfony\Component\HttpFoundation\BinaryFileResponse as BaseBinaryFileResponse;
6
7
class BinaryFileResponse extends BaseBinaryFileResponse
8
{
9
    /**
10
     * @var \Swis\Filesystem\Encrypted\File
11
     */
12
    protected $file;
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 15
    public function setFile($file, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)
18
    {
19
        // Wrap the file in our own \Swis\Filesystem\Encrypted\File class.
20 15
        if (!$file instanceof File) {
21 15
            if ($file instanceof \SplFileInfo) {
22 3
                $file = new File($file->getPathname());
23
            } else {
24 12
                $file = new File((string) $file);
25
            }
26
        }
27
28 15
        return parent::setFile($file, $contentDisposition, $autoEtag, $autoLastModified);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 6
    public function sendContent()
35
    {
36 6
        if (!$this->isSuccessful()) {
37
            return parent::sendContent();
38
        }
39
40 6
        if (0 === $this->maxlen) {
41
            return $this;
42
        }
43
44 6
        $out = fopen('php://output', 'wb');
45
46
        // Create a temporary file stream with the decrypted contents.
47 6
        $file = tmpfile();
48 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

48
        fwrite(/** @scrutinizer ignore-type */ $file, $this->file->getContents());
Loading history...
49 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

49
        fseek(/** @scrutinizer ignore-type */ $file, 0);
Loading history...
50
51 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

51
        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

51
        stream_copy_to_stream($file, /** @scrutinizer ignore-type */ $out, $this->maxlen, $this->offset);
Loading history...
52
53 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

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