Completed
Push — master ( ad721e...9c02d3 )
by Garveen
04:01
created

SwooleFastCGIWrapper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 97.3%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 64
ccs 36
cts 37
cp 0.973
rs 10
c 1
b 1
f 0
wmc 11
lcom 2
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onReceive() 0 4 1
A requestCallback() 0 4 1
A sendCallback() 0 4 1
A onWorkerStart() 0 10 1
A start() 0 16 3
A closeCallback() 0 10 3
1
<?php
2
namespace Laravoole\Wrapper;
3
4
use Garveen\FastCgi\FastCgi;
5
use swoole_server;
6
7
class SwooleFastCGIWrapper extends Swoole implements ServerInterface
8
{
9
    protected $max_request = 0;
10
    protected $request_count = 0;
11
12 4
    public function __construct($host, $port)
13
    {
14 4
        $this->server = new swoole_server($host, $port);
15 4
    }
16
17 4
    public function start()
18
    {
19 4
        if (!empty($this->handler_config)) {
20 4
            if (isset($this->handler_config['max_request'])) {
21 4
                $this->max_request = $this->handler_config['max_request'];
22 2
            }
23 4
            $this->handler_config['max_request'] = 0;
24 4
            $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...
25 2
        }
26
27 4
        $this->callbacks = array_merge([
28 4
            'Receive' => [$this, 'onReceive'],
29 4
        ], $this->callbacks);
30
31 4
        return parent::start();
32
    }
33
34 4
    public function onReceive($serv, $fd, $from_id, $data)
35
    {
36 4
        return $this->fastcgi->receive($fd, $data);
0 ignored issues
show
Bug introduced by
The property fastcgi does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
    }
38
39 4
    public function requestCallback($psrRequest)
40
    {
41 4
        return $this->onPsrRequest($psrRequest);
42
    }
43
44 4
    public function sendCallback($fd, $payload)
45
    {
46 4
        $this->server->send($fd, $payload);
0 ignored issues
show
Bug introduced by
The method send() 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...
47 4
    }
48
49 4
    public function closeCallback($fd)
50
    {
51 4
        $this->server->close($fd);
0 ignored issues
show
Bug introduced by
The method close() 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...
52 4
        if ($this->max_request) {
53 4
            if ($this->request_count++ > $this->max_request) {
54
                $this->server->stop(); // @codeCoverageIgnore
55
            }
56 2
        }
57
58 4
    }
59
60 4
    public function onWorkerStart($serv, $worker_id)
61
    {
62 4
        fwrite(STDOUT, "Swoole worker $worker_id starting\n");
63 4
        parent::onWorkerStart($serv, $worker_id);
64 4
        $this->fastcgi = new FastCgi([$this, 'requestCallback'], [$this, 'sendCallback'], [$this, 'closeCallback'], function ($level, $info) {
65 4
            fwrite(STDOUT, "$level $info");
66 4
        });
67
        // override
68 4
        config(['laravoole.base_config.deal_with_public' => false]);
69 4
    }
70
}
71