Completed
Push — master ( 0287cb...35c8bf )
by Dmitry
04:37
created

Service::remove()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 19
loc 19
ccs 0
cts 16
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
    public function __construct(Client $client, Config $config)
14
    {
15
        $this->client = $client;
16
        $this->name = $config['service'];
17
        if(!$this->name) {
18
            throw new Exception("No service defined in config");
19
        }
20
    }
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
    private function exists($path)
49
    {
50
        $chain = explode('/', $path);
51
        $key = array_pop($chain);
52
        $folder = implode('/', $chain);
53
54
        try {
55
            $this->client->setRoot($folder);
56
            $this->client->get($key);
57
58
        } catch(Exception $e) {
59
            return false;
60
        }
61
62
        return true;
63
    }
64
65 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...
66
    {
67
        $chain = explode('/', $path);
68
69
        $key = array_pop($chain);
70
        $folder = implode('/', $chain);
71
72
        $this->client->setRoot($folder);
73
        try {
74
            $this->client->ls('.');
75
        } catch(Exception $e) {
76
            $this->client->mkdir('.');
77
        }
78
79
        try {
80
            $this->client->get($key);
81
        } catch(Exception $e) {
82
            $this->client->set($key, $value);
83
        }
84
    }
85
86 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...
87
    {
88
        $chain = explode('/', $path);
89
90
        $key = array_pop($chain);
91
        $folder = implode('/', $chain);
92
93
        $this->client->setRoot($folder);
94
        try {
95
            $this->client->ls('.');
96
        } catch(Exception $e) {
97
            $this->client->mkdir('.');
98
        }
99
100
        try {
101
            $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...
102
        } catch(Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
103
        }
104
    }
105
}
106