SwooleHttpWrapper   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Test Coverage

Coverage 90.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 99
ccs 56
cts 62
cp 0.9032
rs 10
wmc 17
lcom 3
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A start() 0 10 2
A onRequest() 0 13 4
B handleStaticFile() 0 28 5
A endResponse() 0 9 2
A onWorkerStart() 0 5 1
A ucHeaders() 0 18 2
1
<?php
2
namespace Laravoole\Wrapper;
3
4
use swoole_http_server;
5
6
use Symfony\Component\HttpFoundation\BinaryFileResponse;
7
8
class SwooleHttpWrapper extends Swoole implements ServerInterface
9
{
10
    use HttpTrait;
11
12 4
    public function __construct($host, $port)
13
    {
14 4
        $this->server = new swoole_http_server($host, $port);
15 4
    }
16
17 8
    public function start()
18
    {
19 8
        if (!empty($this->handler_config)) {
20 8
            $this->server->set($this->handler_config);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Laravoole\Workerman\Worker>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21 4
        }
22 8
        $this->callbacks = array_merge([
23 8
            'Request' => [$this, 'onRequest'],
24 8
        ], $this->callbacks);
25 8
        parent::start();
26
    }
27
28 8
    public function onWorkerStart($serv, $worker_id)
29
    {
30 8
        parent::onWorkerStart($serv, $worker_id);
31 8
        $this->accept_gzip = config('laravoole.base_config.gzip');
32 8
    }
33
34 8
    public function onRequest($request, $response)
35
    {
36
        // convert request
37 8
        $this->ucHeaders($request);
38 8
        if (config('laravoole.base_config.deal_with_public')) {
39 8
            if ($status = $this->handleStaticFile($request, $response)) {
40 4
                return $status;
41
            }
42 4
        }
43
        // provide response callback
44 8
        $illuminateResponse = parent::handleRequest($request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (handleRequest() instead of onRequest()). Are you sure this is correct? If so, you might want to change this to $this->handleRequest().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
45 8
        return $this->handleResponse($response, $illuminateResponse, isset($request->header['Accept-Encoding']) ? $request->header['Accept-Encoding'] : '');
46
    }
47
48 8
    protected function ucHeaders($request)
49
    {
50
        // merge headers into server which ar filted by swoole
51
        // make a new array when php 7 has different behavior on foreach
52 8
        $new_header = [];
53 8
        $uc_header = [];
54 8
        foreach ($request->header as $key => $value) {
55 8
            $new_header['http_' . $key] = $value;
56 8
            $uc_header[ucwords($key, '-')] = $value;
57 4
        }
58 8
        $server = array_merge($request->server, $new_header);
59
60
        // swoole has changed all keys to lower case
61 8
        $server = array_change_key_case($server, CASE_UPPER);
62 8
        $request->server = $server;
63 8
        $request->header = $uc_header;
64 8
        return $request;
65
    }
66
67 8
    protected function handleStaticFile($request, $response)
68
    {
69 8
        static $public_path;
70 8
        if (!$public_path) {
71 8
            $app = $this->app;
72 8
            $public_path = $app->make('path.public');
73
74 4
        }
75
76 8
        $uri = $request->server['REQUEST_URI'];
77 8
        $file = realpath($public_path . $uri);
78 8
        if (is_file($file)) {
79 4
            if (!strncasecmp($file, $uri, strlen($public_path))) {
80
                $response->status(403);
81
                $response->end();
82
            } else {
83 4
                $response->header('Content-Type', get_mime_type($file));
84 4
                if (!filesize($file)) {
85
                    $response->end();
86
                } else {
87 4
                    $response->sendfile($file);
88
                }
89
            }
90 4
            return true;
91
        }
92 8
        return false;
93
94
    }
95
96 8
    public function endResponse($response, $content)
97
    {
98 8
        if (!is_string($content)) {
99 8
            $response->sendfile(realpath($content()));
100 4
        } else {
101
            // send content & close
102 8
            $response->end($content);
103
        }
104 8
    }
105
106
}
107