Completed
Push — master ( 6fbd2d...3c210e )
by Dmitry
02:59
created

Service   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 82
ccs 34
cts 42
cp 0.8095
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A listServices() 0 4 2
A subscribe() 0 7 1
A unsubscribe() 0 7 1
A eventExists() 0 15 4
B eventMatch() 0 15 6
A getHost() 0 6 1
1
<?php
2
3
namespace Basis;
4
5
use Exception;
6
use Tarantool\Mapper\Pool;
7
8
class Service
9
{
10
    private $app;
11
    private $cache;
12
    private $name;
13
14 15
    public function __construct($name, Application $app, Cache $cache)
15
    {
16 15
        $this->app = $app;
17 15
        $this->cache = $cache;
18 15
        $this->name = $name;
19 15
    }
20
21 11
    public function getName() : string
22
    {
23 11
        return $this->name;
24
    }
25
26
    private $services;
27
28 1
    public function listServices() : array
29
    {
30 1
        return $this->services ?: $this->services = $this->app->dispatch('web.services')->services;
31
    }
32
33 6
    public function subscribe(string $event)
34
    {
35 6
        $this->app->dispatch('event.subscribe', [
36 6
            'event' => $event,
37 6
            'service' => $this->getName(),
38
        ]);
39 3
    }
40
41
    public function unsubscribe(string $event)
42
    {
43
        $this->app->dispatch('event.unsubscribe', [
44
            'event' => $event,
45
            'service' => $this->getName(),
46
        ]);
47
    }
48
49
    private $eventExistence = [];
50
51 1
    public function eventExists(string $event) : bool
52
    {
53 1
        if (array_key_exists($event, $this->eventExistence)) {
54
            return $this->eventExistence[$event];
55
        }
56 1
        $types = $this->app->get(Pool::class)->get('event')->find('type');
57
58 1
        foreach ($types as $type) {
59 1
            if ($this->eventMatch($event, $type->nick)) {
60 1
                return $this->eventExistence[$event] = true;
61
            }
62
        }
63
64 1
        return $this->eventExistence[$event] = false;
65
    }
66
67 1
    public function eventMatch($event, $spec)
68
    {
69 1
        if ($spec == $event) {
70 1
            return true;
71 1
        } else if (strpos($spec, '*') !== false) {
72 1
            $spec = explode('.', $spec);
73 1
            $event = explode('.', $event);
74 1
            $valid = true;
75 1
            foreach (range(0, 2) as $part) {
76 1
                $valid = $valid && ($spec[$part] == '*' || $spec[$part] == $event[$part]);
77
            }
78 1
            return $valid;
79
        }
80 1
        return false;
81
    }
82
83 2
    public function getHost($name)
84
    {
85 2
            return gethostbyname($name);
86
        return $this->cache->wrap('service-host-for-' . $name, function() use ($name) {
0 ignored issues
show
Unused Code introduced by
return $this->cache->wra...ion () use($name) { }); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
87
        });
88
    }
89
}
90