Workerman::send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Laravoole\Wrapper;
3
4
use Laravoole\Base;
5
use Exception;
6
use Workerman\Worker;
7
8
abstract class Workerman extends Base implements ServerInterface
9
{
10
    protected $eventMapper = [
11
        'WorkerStart' => 'onWorkerStart',
12
        'WorkerStop' => 'onWorkerStop',
13
        'Connect' => 'onConnect',
14
        'Receive' => 'onMessage',
15
        'Close' => 'onClose',
16
    ];
17
18
    public static function getParams()
19
    {
20
        return [
21
            'name',
22
            'user',
23
            'reloadable',
24
            'transport',
25
            'daemonize',
26
            'stdoutFile',
27
            'reusePort',
28
        ];
29
    }
30
31 8
    public function __construct($host, $port)
32
    {
33 8
        if (file_exists(__DIR__ . '/../../vendor/workerman/workerman/Autoloader.php')) {
34 8
            require __DIR__ . '/../../vendor/workerman/workerman/Autoloader.php';
35 4
        } else {
36
            require __DIR__ . '/../../../../workerman/workerman/Autoloader.php'; // @codeCoverageIgnore
37
        }
38 8
    }
39
40 8
    public function start()
41
    {
42 8
        $this->set(['pidFile' => $this->pid_file]);
43 8
        if (!empty($this->handler_config)) {
44 8
            $this->set($this->handler_config);
45 4
        }
46 8
        $this->on('WorkerStart', [$this, 'onWorkerStart']);
47
48 8
        return $this->server->runAll();
49
    }
50
51 4
    public function send($fd, $content)
52
    {
53 4
        return $this->connections[$fd]['connection']->send($content);
0 ignored issues
show
Bug introduced by
The property connections 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...
54
    }
55
56 4
    public function close($fd)
57
    {
58 4
        return $this->connections[$fd]['connection']->close();
59
    }
60
61 8
    public function onWorkerStart($worker)
62
    {
63 8
        $this->server = $worker;
64 8
        parent::prepareKernel();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (prepareKernel() instead of onWorkerStart()). Are you sure this is correct? If so, you might want to change this to $this->prepareKernel().

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...
65 8
    }
66
67 8
    public function on($event, callable $callback)
68
    {
69 8
        if (!isset($this->eventMapper[$event])) {
70
            throw new Exception("Event $event not exists", 1); // @codeCoverageIgnore
71
        }
72
73 8
        $this->server->{$this->eventMapper[$event]} = $callback;
74 8
        return true;
75
    }
76
77 8
    public function set($settings)
78
    {
79 8
        $server = $this->server;
80 8
        foreach ($settings as $key => $value) {
81 8
            $server::$$key = $value;
82 4
        }
83 8
        return true;
84
    }
85
86
    /**
87
     * @codeCoverageIgnore
88
     */
89
    public function getPid()
90
    {
91
        throw new Exception("Can't read pid from Workerman", 1);
92
    }
93
94
}
95