Client::connected()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Swoole client
4
 * User: moyo
5
 * Date: 27/09/2017
6
 * Time: 4:57 PM
7
 */
8
9
namespace Carno\Socket\Powered\Swoole;
10
11
use Carno\Net\Address;
12
use Carno\Net\Events;
13
use Carno\Socket\Connection;
14
use Carno\Socket\Contracts\TClient;
15
use Carno\Socket\Options;
16
use Carno\Socket\Powered\Swoole\Chips\Buffered;
17
use Swoole\Client as SWClient;
0 ignored issues
show
Bug introduced by
The type Swoole\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class Client implements TClient
20
{
21
    use Buffered;
22
23
    /**
24
     * @var array
25
     */
26
    private $acceptEvs = [
27
        'bufferFull', 'bufferEmpty',
28
        'connect', 'close',
29
        'receive',
30
        'error',
31
    ];
32
33
    /**
34
     * @var Address
35
     */
36
    private $address = null;
37
38
    /**
39
     * @var Events
40
     */
41
    private $events = null;
42
43
    /**
44
     * @var SWClient
45
     */
46
    private $client = null;
47
48
    /**
49
     * @param Address $address
50
     * @param Events $events
51
     * @param Options $options
52
     * @return TClient
53
     */
54
    public function connect(Address $address, Events $events, Options $options = null) : TClient
55
    {
56
        $this->address = $address;
57
        $this->events = $events;
58
59
        $this->client = new SWClient(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
0 ignored issues
show
Bug introduced by
The constant Carno\Socket\Powered\Swoole\SWOOLE_SOCK_TCP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Carno\Socket\Powered\Swoole\SWOOLE_SOCK_ASYNC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
60
61
        $options && $this->client->set($options->config());
62
63
        foreach ($this->acceptEvs as $ev) {
64
            $this->client->on($ev, [$this, sprintf('ev%s', ucfirst($ev))]);
65
        }
66
67
        $this->client->connect($address->host(), $address->port());
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param SWClient $client
74
     */
75
    public function evConnect(SWClient $client) : void
76
    {
77
        $local = $client->getsockname();
78
79
        $this->events->notify(
80
            Events\Socket::CONNECTED,
81
            (new Connection)
82
                ->setLocal($local['host'], $local['port'])
83
                ->setRemote($this->address->host(), $this->address->port())
84
                ->from($this)
85
        );
86
    }
87
88
    /**
89
     * @param SWClient $client
90
     */
91
    public function evClose(SWClient $client) : void
0 ignored issues
show
Unused Code introduced by
The parameter $client 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

91
    public function evClose(/** @scrutinizer ignore-unused */ SWClient $client) : 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...
92
    {
93
        $this->events->notify(
94
            Events\Socket::CLOSED,
95
            (new Connection)
96
                ->setRemote($this->address->host(), $this->address->port())
97
                ->from($this)
98
        );
99
    }
100
101
    /**
102
     * @param SWClient $client
103
     */
104
    public function evError(SWClient $client) : void
0 ignored issues
show
Unused Code introduced by
The parameter $client 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

104
    public function evError(/** @scrutinizer ignore-unused */ SWClient $client) : 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...
105
    {
106
        $this->events->notify(
107
            Events\Socket::ERROR,
108
            (new Connection)
109
                ->setRemote($this->address->host(), $this->address->port())
110
                ->from($this)
111
        );
112
    }
113
114
    /**
115
     * @param SWClient $client
116
     * @param string $data
117
     */
118
    public function evReceive(SWClient $client, string $data) : void
0 ignored issues
show
Unused Code introduced by
The parameter $client 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

118
    public function evReceive(/** @scrutinizer ignore-unused */ SWClient $client, string $data) : 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...
119
    {
120
        $this->events->notify(
121
            Events\Socket::RECEIVED,
122
            (new Connection)
123
                ->setReceived($data)
124
                ->setRemote($this->address->host(), $this->address->port())
125
                ->from($this)
126
        );
127
    }
128
129
    /**
130
     * @param int $conn
131
     * @return bool
132
     */
133
    public function connected(int $conn = 0) : bool
134
    {
135
        return $this->client->isConnected();
136
    }
137
138
    /**
139
     * @param string $data
140
     * @param int $conn
141
     * @return bool
142
     */
143
    public function send(string $data, int $conn = 0) : bool
144
    {
145
        return $this->connected($conn) ? !! $this->client->send($data) : false;
146
    }
147
148
    /**
149
     * @param int $conn
150
     * @return bool
151
     */
152
    public function close(int $conn = 0) : bool
153
    {
154
        return $this->connected($conn) ? $this->client->close() : false;
155
    }
156
}
157