Completed
Push — master ( dc8a1c...bb44ed )
by Dmitry
03:57
created

Service   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 37.14%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 68
ccs 13
cts 35
cp 0.3714
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A listServices() 0 4 2
A subscribe() 0 7 1
A unsubscribe() 0 7 1
A eventExists() 0 12 3
B eventMatch() 0 15 6
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 $name;
12
13 4
    public function __construct($name, Application $app)
14
    {
15 4
        $this->app = $app;
16 4
        $this->name = $name;
17 4
    }
18
19 2
    public function getName() : string
20
    {
21 2
        return $this->name;
22
    }
23
24
    private $services;
25
26 1
    public function listServices() : array
27
    {
28 1
        return $this->services ?: $this->services = $this->app->dispatch('web.services')->names;
29
    }
30
31 1
    public function subscribe(string $event)
32
    {
33 1
        $this->app->dispatch('event.subscribe', [
34 1
            'event' => $event,
35 1
            'name' => $this->getName(),
36
        ]);
37 1
    }
38
39
    public function unsubscribe(string $event)
40
    {
41
        $this->app->dispatch('event.unsubscribe', [
42
            'event' => $event,
43
            'name' => $this->getName(),
44
        ]);
45
    }
46
47
    public function eventExists(string $event) : bool
48
    {
49
        $types = $this->app->get(Pool::class)->get('event')->find('type');
50
51
        foreach ($types as $type) {
52
            if ($this->eventMatch($event, $type->nick)) {
53
                return true;
54
            }
55
        }
56
57
        return false;
58
    }
59
60
    public function eventMatch($event, $spec)
61
    {
62
        if ($spec == $event) {
63
            return true;
64
        } else if (strpos($spec, '*') !== false) {
65
            $spec = explode('.', $spec);
66
            $event = explode('.', $event);
67
            $valid = true;
68
            foreach (range(0, 2) as $part) {
69
                $valid = $valid && ($spec[$part] == '*' || $spec[$part] == $event[$part]);
70
            }
71
            return $valid;
72
        }
73
        return false;
74
    }
75
}
76