Completed
Push — master ( a72680...1055c6 )
by Tomas
16:41 queued 11s
created

GeocoderDataCollector::collect()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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