Completed
Push — develop ( e7703b...db1330 )
by Jens
15:05
created

HandlebarsDataCollector::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
7
namespace JaySDe\HandlebarsBundle\DataCollector;
8
9
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
13
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
14
15
class HandlebarsDataCollector extends DataCollector implements LateDataCollectorInterface
16
{
17
    private $profile;
18
    private $computed;
19
20 10
    public function __construct(\Twig_Profiler_Profile $profile)
21
    {
22 10
        $this->profile = $profile;
23 10
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function collect(Request $request, Response $response, \Exception $exception = null)
29
    {
30
    }
31
32
    public function reset()
33
    {
34
        $this->data = [];
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function lateCollect()
41
    {
42
        $this->data['profile'] = serialize($this->profile);
43
    }
44
45 1
    public function getTime()
46
    {
47 1
        return $this->getProfile()->getDuration() * 1000;
48
    }
49
50 2
    public function getTemplateCount()
51
    {
52 2
        return $this->getComputedData('template_count');
53
    }
54
55 1
    public function getTemplates()
56
    {
57 1
        return $this->getComputedData('templates');
58
    }
59
60 2
    public function getBlockCount()
61
    {
62 2
        return $this->getComputedData('block_count');
63
    }
64
65 2
    public function getMacroCount()
66
    {
67 2
        return $this->getComputedData('macro_count');
68
    }
69
70 1
    public function getHtmlCallGraph()
71
    {
72 1
        $dumper = new \Twig_Profiler_Dumper_Html();
73 1
        $dump = $dumper->dump($this->getProfile());
74
75
        // needed to remove the hardcoded CSS styles
76 1
        $dump = str_replace(array(
77 1
            '<span style="background-color: #ffd">',
78
            '<span style="color: #d44">',
79
            '<span style="background-color: #dfd">',
80
        ), array(
81 1
            '<span class="status-warning">',
82
            '<span class="status-error">',
83
            '<span class="status-success">',
84 1
        ), $dump);
85
86 1
        return new \Twig_Markup($dump, 'UTF-8');
87
    }
88
89 9
    public function getProfile()
90
    {
91 9
        if (null === $this->profile) {
92
            $this->profile = unserialize($this->data['profile']);
93
        }
94
95 9
        return $this->profile;
96
    }
97
98 7
    private function getComputedData($index)
99
    {
100 7
        if (null === $this->computed) {
101 7
            $this->computed = $this->computeData($this->getProfile());
102
        }
103
104 7
        return $this->computed[$index];
105
    }
106
107 7
    private function computeData(\Twig_Profiler_Profile $profile)
108
    {
109
        $data = array(
110 7
            'template_count' => 0,
111
            'block_count' => 0,
112
            'macro_count' => 0,
113
        );
114
115 7
        $templates = array();
116
        /**
117
         * @var \Twig_Profiler_Profile $p
118
         */
119 7
        foreach ($profile as $p) {
120 7
            $d = $this->computeData($p);
121
122 7
            $data['template_count'] += $this->sumProfileCount($p->isTemplate(), $d['template_count']);
123 7
            $data['block_count'] += $this->sumProfileCount($p->isBlock(), $d['block_count']);
124 7
            $data['macro_count'] += $this->sumProfileCount($p->isMacro(), $d['macro_count']);
125
126 7
            $templates = $this->incTemplateCount($p, $templates);
127 7
            $templates = $this->sumTemplates($d['templates'], $templates);
128
        }
129 7
        $data['templates'] = $templates;
130
131 7
        return $data;
132
    }
133
134 7
    protected function incTemplateCount(\Twig_Profiler_Profile $p, $templates)
135
    {
136 7
        if ($p->isTemplate()) {
137 3
            if (!isset($templates[$p->getTemplate()])) {
138 3
                $templates[$p->getTemplate()] = 1;
139
            } else {
140 2
                ++$templates[$p->getTemplate()];
141
            }
142
        }
143 7
        return $templates;
144
    }
145
146 7
    protected function sumProfileCount($pIs, $count)
147
    {
148 7
        return ($pIs ? 1 : 0) + $count;
149
    }
150
151 7
    protected function sumTemplates(array $d, array $templates)
152
    {
153 7
        foreach ($d as $template => $count) {
154 2
            if (!isset($templates[$template])) {
155 2
                $templates[$template] = $count;
156
            } else {
157 2
                $templates[$template] += $count;
158
            }
159
        }
160
161 7
        return $templates;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 1
    public function getName()
168
    {
169 1
        return 'handlebars';
170
    }
171
}
172