EndpointsPersistent   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A staticEndpoint() 0 3 1
A addEndpoint() 0 7 3
A staticEndpoints() 0 3 1
1
<?php
2
/**
3
 * Endpoints that static and persistent
4
 * User: moyo
5
 * Date: 2018/5/16
6
 * Time: 3:05 PM
7
 */
8
9
namespace Carno\NSQ\Chips;
10
11
use Carno\Net\Endpoint;
12
13
trait EndpointsPersistent
14
{
15
    /**
16
     * @var Endpoint[]
17
     */
18
    private $staticEndpoints = [];
19
20
    /**
21
     * @param Endpoint ...$endpoints
22
     * @return static
23
     */
24
    public function addEndpoint(Endpoint ...$endpoints) : self
25
    {
26
        foreach ($endpoints as $endpoint) {
27
            isset($this->staticEndpoints[$i = $endpoint->id()]) || $this->staticEndpoints[$i] = $endpoint;
28
        }
29
30
        return $this;
31
    }
32
33
    /**
34
     * @return Endpoint
35
     */
36
    protected function staticEndpoint() : Endpoint
37
    {
38
        return $this->staticEndpoints[array_rand($this->staticEndpoints)];
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    protected function staticEndpoints() : array
45
    {
46
        return $this->staticEndpoints;
47
    }
48
}
49