Timeline::stream()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.4286
cc 2
eloc 10
nc 2
nop 0
crap 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