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

Service::getHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 2
cts 4
cp 0.5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1.125
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