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

SwooleFastCGIWrapper::closeCallback()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 9.4285
c 0
b 0
f 0
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