StaticResponse   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 34
c 8
b 0
f 0
dl 0
loc 60
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A gzip() 0 2 1
B sendContent() 0 48 9
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Swoole;
4
5
use Symfony\Component\HttpFoundation\BinaryFileResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFo...tion\BinaryFileResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
class StaticResponse extends Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class StaticResponse
Loading history...
8
{
9
    /**@var BinaryFileResponse */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
10
    protected $laravelResponse;
11
12
    public function gzip()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function gzip()
Loading history...
13
    {
14
    }
15
16
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
17
     * @throws \Exception
18
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
19
    public function sendContent()
20
    {
21
        $file = $this->laravelResponse->getFile();
22
        if (!$this->laravelResponse->headers->has('Content-Type')) {
23
            $this->swooleResponse->header('Content-Type', $file->getMimeType());
24
        }
25
        if ($this->laravelResponse->getStatusCode() == BinaryFileResponse::HTTP_NOT_MODIFIED) {
26
            $this->swooleResponse->end();
27
            return;
28
        }
29
30
        $path = $file->getPathname();
31
        $size = filesize($path);
32
        if ($size <= 0) {
33
            $this->swooleResponse->end();
34
            return;
35
        }
36
37
        // Support deleteFileAfterSend: https://github.com/symfony/http-foundation/blob/5.0/BinaryFileResponse.php#L305
38
        $reflection = new \ReflectionObject($this->laravelResponse);
39
        if ($reflection->hasProperty('deleteFileAfterSend')) {
40
            $deleteFileAfterSend = $reflection->getProperty('deleteFileAfterSend');
41
            $deleteFileAfterSend->setAccessible(true);
42
            $deleteFile = $deleteFileAfterSend->getValue($this->laravelResponse);
43
        } else {
44
            $deleteFile = false;
45
        }
46
47
        if ($deleteFile) {
48
            $fp = fopen($path, 'rb');
49
50
            for ($offset = 0, $limit = (int)(0.99 * $this->chunkLimit); $offset < $size; $offset += $limit) {
51
                fseek($fp, $offset, SEEK_SET);
52
                $chunk = fread($fp, $limit);
53
                $this->swooleResponse->write($chunk);
54
            }
55
            $this->swooleResponse->end();
56
57
            fclose($fp);
58
59
            if (file_exists($path)) {
60
                unlink($path);
61
            }
62
        } else {
63
            if (version_compare(SWOOLE_VERSION, '1.7.21', '<')) {
64
                throw new \RuntimeException('sendfile() require Swoole >= 1.7.21');
65
            }
66
            $this->swooleResponse->sendfile($path);
67
        }
68
    }
69
}
70