Timeline   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 2
Metric Value
wmc 4
c 7
b 1
f 2
lcom 1
cbo 7
dl 0
loc 64
ccs 29
cts 29
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerListeners() 0 20 1
A getPath() 0 4 1
A stream() 0 14 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arnaud
5
 * Date: 01/11/15
6
 * Time: 23:17
7
 */
8
9
namespace Ndrx\Profiler\Collectors\Data;
10
11
use Ndrx\Profiler\Collectors\StreamCollector;
12
use Ndrx\Profiler\Events\Timeline\End;
13
use Ndrx\Profiler\Events\Timeline\Start;
14
use Ndrx\Profiler\JsonPatch;
15
16
class Timeline extends StreamCollector
17
{
18
    /**
19
     * @author LAHAXE Arnaud
20
     *
21
     *
22
     */
23 104
    protected function registerListeners()
24
    {
25
        $this->process->getDispatcher()->addListener(Start::EVENT_NAME, function (Start $event) {
26 2
            $this->data = [
27 2
                'key' => md5($event->getKey()),
28 2
                'start' => $event->getTimestamp(),
29 2
                'label' => $event->getLabel(),
30 2
                'data' => $event->getData()
31 2
            ];
32 2
            $this->stream();
33 104
        });
34
35 104
        $this->process->getDispatcher()->addListener(End::EVENT_NAME, function (End $event) {
36 2
            $this->data = [
37 2
                'key' => md5($event->getKey()),
38 2
                'end' => $event->getTimestamp(),
39
            ];
40 2
            $this->stream();
41 104
        });
42 104
    }
43
44
    /**
45
     * The path in the final json
46
     * @example
47
     *  path /aa/bb
48
     *  will be transformed to
49
     *  {
50
     *     aa : {
51
     *              bb: <VALUE OF RESOLVE>
52
     *       }
53
     *  }
54
     * @return mixed
55
     */
56 104
    public function getPath()
57
    {
58 104
        return 'timeline';
59
    }
60
61
    /**
62
     * Write data in the datasource and clean current buffer
63
     * @return mixed
64
     */
65 2
    public function stream()
66
    {
67 2
        $key = $this->data['key'];
68 2
        unset($this->data['key']);
69 2
        $path = $this->getPath() . '/' . $key;
70 2
        if (!array_key_exists('end', $this->data)) {
71 2
            $patch = $this->jsonPatch->generate($path, JsonPatch::ACTION_ADD, $this->data, false);
72 2
        } else {
73 2
            $patch = $this->jsonPatch->generate($path . '/end', JsonPatch::ACTION_ADD, $this->data['end'], false);
74
        }
75
76 2
        $this->dataSource->save($this->process, [$patch]);
77 2
        $this->data = [];
78 2
    }
79
}
80