Passed
Push — master ( b36d44...822816 )
by Biao
04:55
created

StaticResponse::sendContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 16
rs 9.8666
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
                $this->swooleResponse->sendfile($path);
34
            } else {
35
                $this->swooleResponse->end();
36
            }
37
        }
38
    }
39
}