Completed
Push — master ( 86cfb6...ebc154 )
by Dmitry
03:36
created

Service::remove()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 12
nc 4
nop 1
crap 12
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 registerJob($job, $params)
33
    {
34
        $this->store("jobs/$job/params", json_encode($params));
35
        $this->store("jobs/$job/service", $this->name);
36
    }
37
38
    public function subscribe($event)
39
    {
40
        $this->store("events/$event/$this->name");
41
    }
42
43
    public function unsibscribe($event)
44
    {
45
        $this->remove("events/$event/$this->name");
46
    }
47
48
    public function updateAssetsVersion($hash)
49
    {
50
        $this->store("assets/$this->name", $hash);
51
    }
52
53
    private function exists($path)
54
    {
55
        $chain = explode('/', $path);
56
        $key = array_pop($chain);
57
        $folder = implode('/', $chain);
58
59
        try {
60
            $this->client->setRoot($folder);
61
            $this->client->get($key);
62
        } catch (Exception $e) {
63
            return false;
64
        }
65
66
        return true;
67
    }
68
69
    private function store($path, $value = null)
70
    {
71
        $chain = explode('/', $path);
72
73
        $key = array_pop($chain);
74
        $folder = implode('/', $chain);
75
76
        $this->client->setRoot($folder);
77
        try {
78
            $this->client->ls('.');
79
        } catch (Exception $e) {
80
            $this->client->mkdir('.');
81
        }
82
83
        try {
84
            if ($this->client->get($key) != $value) {
85
                $this->client->set($key, $value);
86
            }
87
        } catch (Exception $e) {
88
            $this->client->set($key, $value);
89
        }
90
    }
91
92
    private function remove($path)
93
    {
94
        $chain = explode('/', $path);
95
96
        $key = array_pop($chain);
97
        $folder = implode('/', $chain);
98
99
        $this->client->setRoot($folder);
100
        try {
101
            $this->client->ls('.');
102
        } catch (Exception $e) {
103
            $this->client->mkdir('.');
104
        }
105
106
        try {
107
            $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...
108
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
109
        }
110
    }
111
}
112