KVGetting::result()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
/**
3
 * /kv/:key
4
 * User: moyo
5
 * Date: 13/10/2017
6
 * Time: 6:17 PM
7
 */
8
9
namespace Carno\Consul\APIs;
10
11
use Carno\Consul\Types\KVs;
12
use Carno\HTTP\Standard\Response;
13
use Carno\Promise\Promised;
14
15
class KVGetting extends AbstractWatcher
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $method = 'GET';
21
22
    /**
23
     * @var string
24
     */
25
    protected $uri = '/kv/:path';
26
27
    /**
28
     * custom path
29
     * @var string
30
     */
31
    private $path = null;
32
33
    /**
34
     * @param string $path
35
     * @return static
36
     */
37
    public function path(string $path) : self
38
    {
39
        $this->path = $path;
40
        $this->setVars('path', $path);
41
        $this->setQuery('recurse', true);
42
        return $this;
43
    }
44
45
    /**
46
     * @return Promised|KVs
47
     */
48
    public function result()
49
    {
50
        return $this->perform($this->getCanceller())->then(function (Response $response) {
51
            $kvs = new KVs(
52
                $this->path,
53
                $response->getStatusCode() === 200
54
                    ? $this->decodeResponse((string)$response->getBody())
55
                    : []
56
            );
57
58
            $this->assignVIndex($kvs, $response);
59
            $this->setVIndex($kvs->getVersion());
60
61
            return $kvs;
62
        });
63
    }
64
}
65