Completed
Push — master ( 852192...fe2434 )
by Dmitry
05:04
created

Service   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 8.47%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 109
ccs 5
cts 59
cp 0.0847
rs 10
c 2
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A eventExists() 0 4 1
A getName() 0 4 1
A registerJob() 0 5 1
A registerRoute() 0 4 1
A subscribe() 0 4 1
A unsibscribe() 0 4 1
A updateAssetsVersion() 0 4 1
A exists() 0 15 2
B store() 0 22 4
A remove() 0 19 3
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 registerRoute($route)
39
    {
40
        $this->store("routes/$route", $this->name);
41
    }
42
43
    public function subscribe($event)
44
    {
45
        $this->store("events/$event/$this->name");
46
    }
47
48
    public function unsibscribe($event)
49
    {
50
        $this->remove("events/$event/$this->name");
51
    }
52
53
    public function updateAssetsVersion($hash)
54
    {
55
        $this->store("assets/$this->name", $hash);
56
    }
57
58
    private function exists($path)
59
    {
60
        $chain = explode('/', $path);
61
        $key = array_pop($chain);
62
        $folder = implode('/', $chain);
63
64
        try {
65
            $this->client->setRoot($folder);
66
            $this->client->get($key);
67
        } catch (Exception $e) {
68
            return false;
69
        }
70
71
        return true;
72
    }
73
74
    private function store($path, $value = null)
75
    {
76
        $chain = explode('/', $path);
77
78
        $key = array_pop($chain);
79
        $folder = implode('/', $chain);
80
81
        $this->client->setRoot($folder);
82
        try {
83
            $this->client->ls('.');
84
        } catch (Exception $e) {
85
            $this->client->mkdir('.');
86
        }
87
88
        try {
89
            if ($this->client->get($key) != $value) {
90
                $this->client->set($key, $value);
91
            }
92
        } catch (Exception $e) {
93
            $this->client->set($key, $value);
94
        }
95
    }
96
97
    private function remove($path)
98
    {
99
        $chain = explode('/', $path);
100
101
        $key = array_pop($chain);
102
        $folder = implode('/', $chain);
103
104
        $this->client->setRoot($folder);
105
        try {
106
            $this->client->ls('.');
107
        } catch (Exception $e) {
108
            $this->client->mkdir('.');
109
        }
110
111
        try {
112
            $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...
113
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
114
        }
115
    }
116
}
117