Completed
Pull Request — master (#20)
by GBProd
01:38
created

ElasticaDataCollector::reset()   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 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace GBProd\ElasticaBundle\DataCollector;
4
5
use GBProd\ElasticaBundle\Logger\ElasticaLogger;
6
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
10
/**
11
 * Data collector collecting elastica statistics.
12
 *
13
 * @author gbprod <[email protected]>
14
 */
15
class ElasticaDataCollector extends DataCollector
16
{
17
    /**
18
     * @var ElasticaLogger
19
     */
20
    protected $logger;
21
22
    /**
23
     * @param ElasticaLogger $logger
24
     */
25 7
    public function __construct(ElasticaLogger $logger)
26
    {
27 7
        $this->logger = $logger;
28 7
        $this->reset();
29 7
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 5
    public function collect(Request $request, Response $response, \Exception $exception = null)
35
    {
36 5
        $this->data['nb_queries'] = $this->logger->getNbQueries();
37 5
        $this->data['queries'] = $this->logger->getQueries();
38 5
    }
39
40
    /**
41
     * Nb of queries executed
42
     *
43
     * @return integer
44
     */
45 2
    public function getQueryCount()
46
    {
47 2
        return $this->data['nb_queries'];
48
    }
49
50
    /**
51
     * Queries
52
     *
53
     * @return array
54
     */
55 2
    public function getQueries()
56
    {
57 2
        return $this->data['queries'];
58
    }
59
60
    /**
61
     * Execution time
62
     *
63
     * @return integer
64
     */
65 2
    public function getTime()
66
    {
67 2
        $time = 0;
68 2
        foreach ($this->data['queries'] as $query) {
69 2
            if (array_key_exists('took', $query['response'])) {
70 2
                $time += $query['response']['took'];
71 2
            }
72 2
        }
73
74 2
        return $time;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 1
    public function getName()
81
    {
82 1
        return 'elastica';
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 7
    public function reset()
89
    {
90 7
        $this->data = [
91 7
            'nb_queries' => 0,
92 7
            'queries' => [],
93
        ];
94 7
    }
95
}
96