Completed
Push — master ( 554b4a...dd5b67 )
by Dmitry
04:04
created

Service::exists()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 10
nc 3
nop 1
crap 6
1
<?php
2
3
namespace Basis;
4
5
use Exception;
6
use LinkORB\Component\Etcd\Client;
7
8
class Service
9
{
10
    private $client;
11
    private $name;
12
13 1
    public function __construct(Client $client, Config $config)
14
    {
15 1
        $this->client = $client;
16 1
        $this->name = $config['service'];
17 1
        if (!$this->name) {
18
            throw new Exception("No service defined in config");
19
        }
20 1
    }
21
22
    public function eventExists($event)
23
    {
24
        return $this->exists("events/$event");
25
    }
26
27
    public function getName()
28
    {
29
        return $this->name;
30
    }
31
32
    public function register()
33
    {
34
        $this->store("services/$this->name");
35
    }
36
37
    public function listServices()
38
    {
39
        $this->client->setRoot('services');
40
41
        $services = [];
42
        foreach ($this->client->ls() as $entry) {
43
            $name = substr($entry, strlen('/services/'));
44
            if ($name) {
45
                $services[] = $name;
46
            }
47
        }
48
49
        return $services;
50
    }
51
52
    public function registerJob($job, $params)
53
    {
54
        $this->store("jobs/$job/params", json_encode($params));
55
        $this->store("jobs/$job/service", $this->name);
56
    }
57
58
    public function registerRoute($route)
59
    {
60
        $this->store("routes/$route", $this->name);
61
    }
62
63
    public function subscribe($event)
64
    {
65
        $this->store("events/$event/$this->name");
66
    }
67
68
    public function unsibscribe($event)
69
    {
70
        $this->remove("events/$event/$this->name");
71
    }
72
73
    private function exists($path)
74
    {
75
        $chain = explode('/', $path);
76
        $key = array_pop($chain);
77
        $folder = implode('/', $chain);
78
79
        try {
80
            $this->client->setRoot($folder);
81
            $this->client->get($key);
82
        } catch (Exception $e) {
83
            return false;
84
        }
85
86
        return true;
87
    }
88
89
    private function store($path, $value = null)
90
    {
91
        $chain = explode('/', $path);
92
93
        $key = array_pop($chain);
94
        $folder = implode('/', $chain);
95
96
        $this->client->setRoot($folder);
97
        try {
98
            $this->client->ls('.');
99
        } catch (Exception $e) {
100
            $this->client->mkdir('.');
101
        }
102
103
        try {
104
            if ($this->client->get($key) != $value) {
105
                $this->client->set($key, $value);
106
            }
107
        } catch (Exception $e) {
108
            $this->client->set($key, $value);
109
        }
110
    }
111
112
    private function remove($path)
113
    {
114
        $chain = explode('/', $path);
115
116
        $key = array_pop($chain);
117
        $folder = implode('/', $chain);
118
119
        $this->client->setRoot($folder);
120
        try {
121
            $this->client->ls('.');
122
        } catch (Exception $e) {
123
            $this->client->mkdir('.');
124
        }
125
126
        try {
127
            $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...
128
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
129
        }
130
    }
131
}
132