SEndpoints::addEndpoint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Service endpoints
4
 * User: moyo
5
 * Date: 25/09/2017
6
 * Time: 11:49 AM
7
 */
8
9
namespace Carno\Consul\Chips;
10
11
use Carno\Consul\Exception\NoneEndpointException;
12
use Carno\Net\Endpoint;
13
14
trait SEndpoints
15
{
16
    /**
17
     * @var Endpoint[]
18
     */
19
    private $endpoints = [];
20
21
    /**
22
     * @return Endpoint
23
     */
24
    public function endpoint() : Endpoint
25
    {
26
        if (is_null($endpoint = $this->endpoints[0] ?? null)) {
27
            throw new NoneEndpointException;
28
        } else {
29
            return $endpoint;
30
        }
31
    }
32
33
    /**
34
     * @return Endpoint|null
35
     */
36
    public function getEndpoint() : ?Endpoint
37
    {
38
        return $this->endpoints[array_rand($this->endpoints)] ?? null;
39
    }
40
41
    /**
42
     * @return Endpoint[]
43
     */
44
    public function getEndpoints() : array
45
    {
46
        return $this->endpoints;
47
    }
48
49
    /**
50
     * @param Endpoint ...$eps
51
     * @return self
52
     */
53
    public function addEndpoint(Endpoint ...$eps) : self
54
    {
55
        $this->endpoints = array_merge($this->endpoints, $eps);
56
        return $this;
57
    }
58
59
    /**
60
     * @param Endpoint ...$eps
61
     * @return self
62
     */
63
    public function setEndpoints(Endpoint ...$eps) : self
64
    {
65
        $this->endpoints = $eps;
66
        return $this;
67
    }
68
}
69