Completed
Push — develop ( 973ef3...3a77eb )
by Jens
03:33
created

HandlebarsProfileExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
7
namespace JaySDe\HandlebarsBundle;
8
9
10
use Symfony\Component\Stopwatch\Stopwatch;
11
12
class HandlebarsProfileExtension
13
{
14
    private $actives = array();
15
    private $stopwatch;
16
    private $events;
17
18 2
    public function __construct(\Twig_Profiler_Profile $profile, Stopwatch $stopwatch = null)
19
    {
20 2
        $this->actives[] = $profile;
21
22 2
        $this->stopwatch = $stopwatch;
23 2
        $this->events = new \SplObjectStorage();
24 2
    }
25
26 1
    public function enter(\Twig_Profiler_Profile $profile)
27
    {
28 1
        if ($this->stopwatch && $profile->isTemplate()) {
29 1
            $this->events[$profile] = $this->stopwatch->start($profile->getName(), 'template');
30
        }
31
32 1
        $this->actives[0]->addProfile($profile);
33 1
        array_unshift($this->actives, $profile);
34 1
    }
35
36 1
    public function leave(\Twig_Profiler_Profile $profile)
37
    {
38 1
        $profile->leave();
39 1
        array_shift($this->actives);
40
41 1
        if (1 === count($this->actives)) {
42 1
            $this->actives[0]->leave();
43
        }
44 1
        if ($this->stopwatch && $profile->isTemplate()) {
45 1
            $this->events[$profile]->stop();
46 1
            unset($this->events[$profile]);
47
        }
48 1
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function getName()
54
    {
55 1
        return 'profiler';
56
    }
57
}
58