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

Service   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 98
Duplicated Lines 39.8 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 1
cbo 1
dl 39
loc 98
ccs 0
cts 75
cp 0
rs 10

9 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 subscribe() 0 4 1
A unsibscribe() 0 4 1
A exists() 0 16 2
A store() 20 20 3
A remove() 19 19 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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