Completed
Push — master ( 8d4298...1c42b2 )
by Oliver
7s
created

ClientProfiler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 72
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A startProfiling() 0 14 1
A stopProfiling() 0 17 1
A getCalls() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the MobileNotifBundle package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace LinkValue\MobileNotifBundle\Profiler;
11
12
use Symfony\Component\Stopwatch\Stopwatch;
13
use Symfony\Component\Stopwatch\StopwatchEvent;
14
15
/**
16
 * ClientProfiler.
17
 *
18
 * @package MobileNotifBundle
19
 * @author  Jamal Youssefi <[email protected]>
20
 * @author  Valentin Coulon <[email protected]>
21
 */
22
class ClientProfiler implements ClientProfilerInterface
23
{
24
    /**
25
     * @var array
26
     */
27
    private $calls;
28
29
    /**
30
     * @var int
31
     */
32
    private $counter;
33
34
    /**
35
     * @param Stopwatch $stopwatch
36
     */
37
    public function __construct(Stopwatch $stopwatch)
38
    {
39
        $this->calls = array();
40
        $this->counter = 0;
41
        $this->stopwatch = $stopwatch;
0 ignored issues
show
Bug introduced by
The property stopwatch does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
    }
43
44
    /**
45
     * @param $message
46
     *
47
     * @return StopwatchEvent
48
     */
49
    public function startProfiling($message)
50
    {
51
        $this->calls[$this->counter] = array(
52
            'message' => $message,
53
            'duration' => null,
54
            'memory_start' => memory_get_usage(true),
55
            'memory_end' => null,
56
            'memory_use' => null,
57
            'error' => null,
58
            'error_message' => null,
59
        );
60
61
        return $this->stopwatch->start($message);
62
    }
63
64
    /**
65
     * @param StopwatchEvent $event
66
     * @param $result
67
     */
68
    public function stopProfiling(StopwatchEvent $event, $result)
69
    {
70
        $event->stop();
71
72
        $memory = memory_get_usage(true);
73
        $values = array(
74
            'duration' => $event->getDuration(),
75
            'memory_end' => $memory,
76
            'memory_use' => $memory - $this->calls[$this->counter]['memory_start'],
77
            'error' => $result['error'],
78
            'error_message' => $result['error_message'],
79
        );
80
81
        $this->calls[$this->counter] = array_merge($this->calls[$this->counter], $values);
82
83
        ++$this->counter;
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function getCalls()
90
    {
91
        return $this->calls;
92
    }
93
}
94