Linker::connect()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nsqd linker with pool
4
 * User: moyo
5
 * Date: 27/02/2018
6
 * Time: 11:49 AM
7
 */
8
9
namespace Carno\NSQ\Connector;
10
11
use Carno\Net\Endpoint;
12
use Carno\NSQ\Types\Consuming;
13
use Carno\Pool\Options;
14
use Carno\Pool\Pool;
15
use Carno\Pool\Wrapper\SAR;
16
use Carno\Promise\Promise;
17
use Carno\Promise\Promised;
18
19
class Linker
20
{
21
    use SAR;
22
23
    // node joining
24
    public const ACT_JOIN = 0xE1;
25
26
    // node leaving
27
    public const ACT_LEAVE = 0xE9;
28
29
    /**
30
     * @var Pool
31
     */
32
    private $pool = null;
33
34
    /**
35
     * @var Nsqd
36
     */
37
    private $nsqd = null;
38
39
    /**
40
     * Linker constructor.
41
     * @param Endpoint $endpoint
42
     * @param Consuming $consuming
43
     */
44
    public function __construct(Endpoint $endpoint, Consuming $consuming = null)
45
    {
46
        if (is_null($consuming)) {
47
            $this->pool = new Pool(new Options, function () use ($endpoint) {
48
                return new Nsqd($endpoint);
49
            }, "nsqd:{$endpoint->service()}");
50
        } else {
51
            $this->nsqd = new Nsqd($endpoint, $consuming);
52
        }
53
    }
54
55
    /**
56
     * @return Promised
57
     */
58
    public function connect() : Promised
59
    {
60
        return $this->pool ? Promise::resolved() : $this->nsqd->connect();
61
    }
62
63
    /**
64
     * @return Promised
65
     */
66
    public function disconnect() : Promised
67
    {
68
        return $this->pool ? $this->pool->shutdown() : $this->nsqd->close();
69
    }
70
71
    /**
72
     * @param string $name
73
     * @param array $arguments
74
     * @return mixed
75
     */
76
    public function __call(string $name, array $arguments)
77
    {
78
        return $this->sarRun($this->pool, $name, $arguments);
79
    }
80
}
81