Buffered::ifWritable()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 21
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Buffer manager
4
 * User: moyo
5
 * Date: 2018/8/2
6
 * Time: 11:24 AM
7
 */
8
9
namespace Carno\Socket\Powered\Swoole\Chips;
10
11
use Carno\Net\Contracts\TCP;
12
use Carno\Promise\Promise;
13
use Carno\Promise\Promised;
14
use Closure;
15
16
trait Buffered
17
{
18
    /**
19
     * @var bool[]
20
     */
21
    private $writable = [];
22
23
    /**
24
     * @var Promised[]
25
     */
26
    private $waits = [];
27
28
    /**
29
     * @param object $sw
30
     * @param int $conn
31
     */
32
    public function evBufferFull(object $sw, int $conn = 0) : void
0 ignored issues
show
Unused Code introduced by
The parameter $sw is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

32
    public function evBufferFull(/** @scrutinizer ignore-unused */ object $sw, int $conn = 0) : void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        $this->writable[$conn] = false;
35
    }
36
37
    /**
38
     * @param object $sw
39
     * @param int $conn
40
     */
41
    public function evBufferEmpty(object $sw, int $conn = 0) : void
0 ignored issues
show
Unused Code introduced by
The parameter $sw is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    public function evBufferEmpty(/** @scrutinizer ignore-unused */ object $sw, int $conn = 0) : void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        $this->writable[$conn] = true;
44
        if (isset($this->waits[$conn])) {
45
            $wait = $this->waits[$conn];
46
            unset($this->waits[$conn]);
47
            $wait->resolve($this, $conn);
48
        }
49
    }
50
51
    /**
52
     * @param Closure $do
53
     * @param int $conn
54
     * @return bool
55
     */
56
    public function ifWritable(Closure $do, int $conn = 0) : bool
57
    {
58
        if (!$this->connected($conn)) {
0 ignored issues
show
Bug introduced by
It seems like connected() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        if (!$this->/** @scrutinizer ignore-call */ connected($conn)) {
Loading history...
59
            return false;
60
        }
61
62
        if ($this->writable[$conn] ?? true) {
63
            $do($this, $conn);
64
            return true;
65
        }
66
67
        ($this->waits[$conn] ?? $this->waits[$conn] = Promise::deferred())
68
            ->then(function (object $sock, int $conn) use ($do) {
69
                /**
70
                 * @var static $sock
71
                 */
72
                $sock->ifWritable($do, $conn);
73
            })
74
        ;
75
76
        return false;
77
    }
78
79
    /**
80
     * @see TCP::write
81
     * @param string $data
82
     * @param int $conn
83
     * @return bool
84
     */
85
    public function write(string $data, int $conn = 0) : bool
86
    {
87
        return $this->ifWritable(static function (TCP $sock, int $conn) use ($data) {
88
            return $sock->send($data, $conn);
89
        }, $conn);
90
    }
91
}
92