Completed
Push — master ( 346e09...46a5ae )
by Dmitry
06:10
created

Etcd::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Basis;
4
5
use Exception;
6
use LinkORB\Component\Etcd\Client;
7
8
class Etcd
9
{
10
    private $client;
11
    private $service;
12
13
    public function __construct(Client $client, Config $config)
14
    {
15
        $this->client = $client;
16
        $this->service = $config['name'];
17
    }
18
19
    public function registerService()
20
    {
21
        $this->store("services/$this->service/host", strtoupper($this->service).'_SERVICE_HOST');
22
        $this->store("services/$this->service/port", strtoupper($this->service).'_SERVICE_PORT');
23
    }
24
25
    public function registerJob($job, $params)
26
    {
27
        $this->store("jobs/$job/params", json_encode($params));
28
        $this->store("jobs/$job/service", $this->service);
29
    }
30
31
    public function subscribe($event)
32
    {
33
        $this->store("events/$event/$this->service");
34
    }
35
36
    public function unsibscribe($event)
37
    {
38
        $this->remove("events/$event/$this->service");
39
    }
40
41 View Code Duplication
    private function store($path, $value = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $chain = explode('/', $path);
44
45
        $key = array_pop($chain);
46
        $folder = implode('/', $chain);
47
48
        $this->client->setRoot($folder);
49
        try {
50
            $this->client->ls('.');
51
        } catch(Exception $e) {
52
            $this->client->mkdir('.');
53
        }
54
55
        try {
56
            $this->client->get($key);
57
        } catch(Exception $e) {
58
            $this->client->set($key, $value);
59
        }
60
    }
61
62 View Code Duplication
    private function remove($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        $chain = explode('/', $path);
65
66
        $key = array_pop($chain);
67
        $folder = implode('/', $chain);
68
69
        $this->client->setRoot($folder);
70
        try {
71
            $this->client->ls('.');
72
        } catch(Exception $e) {
73
            $this->client->mkdir('.');
74
        }
75
76
        try {
77
            $this->client->remove($key);
0 ignored issues
show
Bug introduced by
The method remove() does not seem to exist on object<LinkORB\Component\Etcd\Client>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
        } catch(Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
79
        }
80
    }
81
}
82