ElasticaDataCollector   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 80
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A collect() 0 5 1
A getQueryCount() 0 4 1
A getQueries() 0 4 1
A getTime() 0 11 3
A getName() 0 4 1
A reset() 0 7 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 10
    public function __construct(ElasticaLogger $logger)
26
    {
27 10
        $this->logger = $logger;
28 10
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 6
    public function collect(Request $request, Response $response, \Exception $exception = null)
34
    {
35 6
        $this->data['nb_queries'] = $this->logger->getNbQueries();
36 6
        $this->data['queries'] = $this->logger->getQueries();
37 6
    }
38
39
    /**
40
     * Nb of queries executed
41
     *
42
     * @return integer
43
     */
44 3
    public function getQueryCount()
45
    {
46 3
        return $this->data['nb_queries'];
47
    }
48
49
    /**
50
     * Queries
51
     *
52
     * @return array
53
     */
54 2
    public function getQueries()
55
    {
56 2
        return $this->data['queries'];
57
    }
58
59
    /**
60
     * Execution time
61
     *
62
     * @return integer
63
     */
64 3
    public function getTime()
65
    {
66 3
        $time = 0;
67 3
        foreach ($this->data['queries'] as $query) {
68 2
            if (array_key_exists('took', $query['response'])) {
69 2
                $time += $query['response']['took'];
70 2
            }
71 3
        }
72
73 3
        return $time;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 4
    public function getName()
80
    {
81 4
        return 'elastica';
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function reset()
88
    {
89 1
        $this->data = [
90 1
            'nb_queries' => 0,
91 1
            'queries' => [],
92
        ];
93 1
    }
94
}
95