AgentServiceRegister::service()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * /agent/service/register
4
 * User: moyo
5
 * Date: 24/08/2017
6
 * Time: 4:54 PM
7
 */
8
9
namespace Carno\Consul\APIs;
10
11
use Carno\Consul\Types\Result;
12
use Carno\Consul\Types\Service;
13
use Carno\Promise\Promised;
14
15
class AgentServiceRegister extends AbstractGate
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $method = 'PUT';
21
22
    /**
23
     * @var string
24
     */
25
    protected $uri = '/agent/service/register';
26
27
    /**
28
     * @var Service
29
     */
30
    private $service = null;
31
32
    /**
33
     * @param Service $service
34
     * @return self
35
     */
36
    public function service(Service $service) : self
37
    {
38
        $this->service = $service;
39
40
        $endpoint = $service->endpoint();
41
42
        $this->setPayload([
43
            'ID' => $service->id(),
44
            'Name' => $service->name(),
45
            'Tags' => $endpoint->getTags(),
46
            'Address' => $endpoint->address()->host(),
47
            'Port' => $endpoint->address()->port(),
48
            'Check' => $service->getKeepalive(),
49
        ]);
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return Promised|Result
56
     */
57
    public function result()
58
    {
59
        return $this->simpleHCodeResult()->then(function (Result $result) {
60
            return $this->service->registered($this->agent(), $this->assigned(), $result);
61
        });
62
    }
63
}
64