Completed
Pull Request — master (#1413)
by
unknown
05:55
created

ElasticaDataCollector::getErrorCount()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 12
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\DataCollector;
13
14
use FOS\ElasticaBundle\Logger\ElasticaLogger;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
18
19
/**
20
 * Data collector collecting elastica statistics.
21
 *
22
 * @author Gordon Franke <[email protected]>
23
 */
24
class ElasticaDataCollector extends DataCollector
25
{
26
    protected $logger;
27
28 5
    public function __construct(ElasticaLogger $logger)
29
    {
30 5
        $this->logger = $logger;
31 5
    }
32
33 4
    public function collect(Request $request, Response $response, \Exception $exception = null)
34
    {
35 4
        $this->data['nb_queries'] = $this->logger->getNbQueries();
36 4
        $this->data['queries'] = $this->logger->getQueries();
37 4
    }
38
39
    /**
40
     * @return mixed
41
     */
42 2
    public function getQueryCount()
43
    {
44 2
        return $this->data['nb_queries'];
45
    }
46
47
    /**
48
     * @return mixed
49
     */
50 2
    public function getQueries()
51
    {
52 2
        return $this->data['queries'];
53
    }
54
55
    /**
56
     * @return int
57
     */
58 2
    public function getTime()
59
    {
60 2
        $time = 0;
61 2
        foreach ($this->data['queries'] as $query) {
62 2
            $time += $query['engineMS'];
63
        }
64
65 2
        return $time;
66
    }
67
68
    /**
69
     * @return int
70
     */
71 1
    public function getExecutionTime()
72
    {
73 1
        $time = 0;
74 1
        foreach ($this->data['queries'] as $query) {
75 1
            $time += $query['executionMS'];
76
        }
77
78 1
        return $time;
79
    }
80
81 1
    public function getName()
82
    {
83 1
        return 'elastica';
84
    }
85
86
    public function reset()
87
    {
88
        $this->logger->reset();
89
        $this->data = [];
90
    }
91
92
    /**
93
     * @return int
94
     */
95
    public function getErrorCount()
96
    {
97
        $errors = 0;
98
        foreach ($this->data['queries'] as $query) {
99
            if ($query['exceptionMessage']) {
100
                $errors++;
101
            }
102
        }
103
104
        return $errors;
105
    }
106
}
107