Completed
Push — master ( 9d8998...346ad6 )
by Dmitry
03:39
created

Service::eventMatch()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 6
nop 2
crap 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 6
    public function __construct($name, Application $app)
14
    {
15 6
        $this->app = $app;
16 6
        $this->name = $name;
17 6
    }
18
19 4
    public function getName() : string
20
    {
21 4
        return $this->name;
22
    }
23
24
    private $services;
25
26 3
    public function listServices() : array
27
    {
28 3
        return $this->services ?: $this->services = $this->app->dispatch('web.services')->names;
29
    }
30
31 2
    public function subscribe(string $event)
32
    {
33 2
        $this->app->dispatch('event.subscribe', [
34 2
            'event' => $event,
35 2
            'name' => $this->getName(),
36
        ]);
37 2
    }
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 1
    public function eventExists(string $event) : bool
48
    {
49 1
        $types = $this->app->get(Pool::class)->get('event')->find('type');
50
51 1
        foreach ($types as $type) {
52 1
            if ($this->eventMatch($event, $type->nick)) {
53 1
                return true;
54
            }
55
        }
56
57 1
        return false;
58
    }
59
60 1
    public function eventMatch($event, $spec)
61
    {
62 1
        if ($spec == $event) {
63 1
            return true;
64 1
        } else if (strpos($spec, '*') !== false) {
65 1
            $spec = explode('.', $spec);
66 1
            $event = explode('.', $event);
67 1
            $valid = true;
68 1
            foreach (range(0, 2) as $part) {
69 1
                $valid = $valid && ($spec[$part] == '*' || $spec[$part] == $event[$part]);
70
            }
71 1
            return $valid;
72
        }
73 1
        return false;
74
    }
75
}
76