INSLinker::linker()   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
 * NSQ instance linker
4
 * User: moyo
5
 * Date: 26/02/2018
6
 * Time: 3:13 PM
7
 */
8
9
namespace Carno\NSQ\Chips;
10
11
use Carno\Net\Endpoint;
12
use Carno\NSQ\Connector\Linker;
13
use Carno\NSQ\Types\Consuming;
14
use Closure;
15
use Throwable;
16
17
trait INSLinker
18
{
19
    /**
20
     * @var Linker[]
21
     */
22
    private $linked = [];
23
24
    /**
25
     * @param Endpoint $endpoint
26
     * @param Consuming $consuming
27
     * @return Linker
28
     */
29
    protected function linking(Endpoint $endpoint, Consuming $consuming = null) : Linker
30
    {
31
        return
32
            $this->linked[(string)$endpoint->address()] ??
33
            $this->linked[(string)$endpoint->address()] = new Linker($endpoint, $consuming);
34
    }
35
36
    /**
37
     * @param Endpoint $endpoint
38
     * @return Linker
39
     */
40
    protected function linker(Endpoint $endpoint) : Linker
41
    {
42
        return $this->linked[(string)$endpoint->address()];
43
    }
44
45
    /**
46
     * @param Closure $operator
47
     */
48
    protected function linkers(Closure $operator) : void
49
    {
50
        foreach ($this->linked as $linker) {
51
            try {
52
                $operator($linker);
53
            } catch (Throwable $e) {
54
                logger('nsq')->notice(
55
                    'Linker operating failed',
56
                    ['error' => sprintf('%s::%s', get_class($e), $e->getMessage())]
57
                );
58
            }
59
        }
60
    }
61
}
62