Passed
Push — master ( 0a340e...b36d44 )
by Biao
04:30
created

StaticResponse::sendContent()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 19
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 28
rs 8.8333
1
<?php
2
3
namespace Hhxsv5\LaravelS\Swoole;
4
5
use Symfony\Component\HttpFoundation\BinaryFileResponse;
6
use Symfony\Component\HttpFoundation\File\File;
7
8
class StaticResponse extends Response
9
{
10
    /**@var BinaryFileResponse */
11
    protected $laravelResponse;
12
13
    public function gzip()
14
    {
15
    }
16
17
    /**
18
     * @throws \Exception
19
     */
20
    public function sendContent()
21
    {
22
        /**@var File $file */
23
        $file = $this->laravelResponse->getFile();
24
        $this->swooleResponse->header('Content-Type', $file->getMimeType());
25
        if ($this->laravelResponse->getStatusCode() == BinaryFileResponse::HTTP_NOT_MODIFIED) {
26
            $this->swooleResponse->end();
27
        } else {
28
            $path = $file->getRealPath();
29
            if (filesize($path) > 0) {
30
                if (version_compare(swoole_version(), '1.7.21', '<')) {
31
                    throw new \RuntimeException('sendfile() require Swoole >= 1.7.21');
32
                }
33
34
                $this->swooleResponse->sendfile($path);
35
36
                // Support deleteFileAfterSend: https://github.com/symfony/http-foundation/blob/5.0/BinaryFileResponse.php#L305
37
                $reflection = new \ReflectionObject($this->laravelResponse);
38
                try {
39
                    $deleteFileAfterSend = $reflection->getProperty('deleteFileAfterSend');
40
                    $deleteFileAfterSend->setAccessible(true);
41
                    if ($deleteFileAfterSend->getValue($this->laravelResponse) && file_exists($file->getPathname())) {
42
                        unlink($file->getPathname());
43
                    }
44
                } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
45
                }
46
            } else {
47
                $this->swooleResponse->end();
48
            }
49
        }
50
    }
51
}