Completed
Push — master ( 392de1...22df44 )
by Tobias
22:17
created

GeocoderDataCollector::getTotalDuration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
/**
4
 * This file is part of the BazingaGeocoderBundle package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @license    MIT License
9
 */
10
11
namespace Bazinga\Bundle\GeocoderBundle\DataCollector;
12
13
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @author Michal Dabrowski <[email protected]>
19
 */
20
class GeocoderDataCollector extends DataCollector
21
{
22
    /**
23
     * @var ProfilingProvider[]
24
     */
25
    private $instances = [];
26
27
28
    public function __construct()
29
    {
30
        $this->data['queries'] = [];
31
        $this->data['providers'] = [];
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function collect(Request $request, Response $response, \Exception $exception = null)
38
    {
39
        foreach ($this->instances as $instance) {
40
            foreach ($instance->getQueries() as $query) {
41
                $query['query'] = $this->cloneVar($query['query']);
42
                $query['result'] = $this->cloneVar($query['result']);
43
                $this->data['queries'][] = $query;
44
            }
45
        }
46
    }
47
48
    /**
49
     * Returns an array of collected requests.
50
     *
51
     * @return array
52
     */
53
    public function getQueries(): array
54
    {
55
        return $this->data['queries'];
56
    }
57
58
    /**
59
     * Returns the execution time of all collected requests in seconds.
60
     *
61
     * @return float
62
     */
63
    public function getTotalDuration()
64
    {
65
        $time = 0;
66
        foreach ($this->data['queries'] as $command) {
67
            $time += $command['duration'];
68
        }
69
70
        return $time;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getProviders(): array
77
    {
78
        return $this->data['providers'];
79
    }
80
81
    /**
82
     * @param string $provider
83
     *
84
     * @return array
85
     */
86
    public function getProviderQueries($provider):array
87
    {
88
        return array_filter($this->data['queries'], function ($data) use ($provider) {
89
            return $data['providerName'] === $provider;
90
        });
91
    }
92
93
    /**
94
     * @param ProfilingProvider $instance
95
     */
96
    public function addInstance(ProfilingProvider $instance)
97
    {
98
        $this->instances[] = $instance;
99
        $this->data['providers'][] = $instance->getName();
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getName()
106
    {
107
        return 'geocoder';
108
    }
109
}
110