Completed
Push — master ( 4f8c6b...1b4351 )
by Dmitry
04:08
created

Bootstrap   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 0
cbo 4
dl 0
loc 41
ccs 0
cts 32
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 20 6
A store() 0 17 3
1
<?php
2
3
namespace Basis\Jobs\Module;
4
5
use Basis\Config;
6
use Basis\Event;
7
use Basis\Runner;
8
use Exception;
9
use LinkORB\Component\Etcd\Client;
10
11
class Bootstrap
12
{
13
    public function run(Runner $runner, Client $client, Config $config, Event $event)
14
    {
15
        $runner->dispatch('tarantool.migrate');
16
17
        $this->store($client, 'services/'.$config['name']);
18
19
        $meta = $runner->dispatch('module.meta');
20
        foreach($meta['jobs'] as $job) {
21
            $this->store($client, 'jobs/'.$job, $config['name']);
22
        }
23
24
        if($config->offsetExists('events') && is_array($config['events'])) {
25
            foreach($config['events'] as $nick => $listeners) {
26
                $listeners = (array) $listeners;
27
                foreach($listeners as $listener) {
28
                    $event->subscribe($nick, $listener);
29
                }
30
            }
31
        }
32
    }
33
34
    private function store(Client $client, $path, $value = null)
35
    {
36
        list($folder, $key) = explode('/', $path);
37
38
        $client->setRoot($folder);
39
        try {
40
            $client->ls('.');
41
        } catch(Exception $e) {
42
            $client->mkdir('.');
43
        }
44
45
        try {
46
            $client->get($key);
47
        } catch(Exception $e) {
48
            $client->set($key, $value);
49
        }
50
    }
51
}
52