-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathProfilingProvider.php
111 lines (94 loc) · 2.79 KB
/
ProfilingProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/*
* This file is part of the BazingaGeocoderBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Bazinga\Bundle\GeocoderBundle\DataCollector;
use Geocoder\Collection;
use Geocoder\Exception\LogicException;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
/**
* @author Tobias Nyholm <[email protected]>
*/
class ProfilingProvider implements Provider
{
/**
* @var Provider
*/
private $realProvider;
/**
* @var array
*/
private $queries = [];
/**
* @param Provider $realProvider
*/
public function __construct(Provider $realProvider)
{
$this->realProvider = $realProvider;
}
public function geocodeQuery(GeocodeQuery $query): Collection
{
$startTime = microtime(true);
try {
$result = $this->realProvider->geocodeQuery($query);
} finally {
$duration = (microtime(true) - $startTime) * 1000;
$this->logQuery($query, $duration, $result);
}
return $result;
}
public function reverseQuery(ReverseQuery $query): Collection
{
$startTime = microtime(true);
try {
$result = $this->realProvider->reverseQuery($query);
} finally {
$duration = (microtime(true) - $startTime) * 1000;
$this->logQuery($query, $duration, $result);
}
return $result;
}
/**
* @param GeocodeQuery|ReverseQuery $query
* @param float $duration geocoding duration
* @param Collection $result
*/
private function logQuery($query, float $duration, Collection $result = null)
{
if ($query instanceof GeocodeQuery) {
$queryString = $query->getText();
} elseif ($query instanceof ReverseQuery) {
$queryString = sprintf('(%s, %s)', $query->getCoordinates()->getLongitude(), $query->getCoordinates()->getLongitude());
} else {
throw new LogicException('First parameter to ProfilingProvider::logQuery must be a query');
}
$this->queries[] = [
'query' => $query,
'queryString' => $queryString,
'duration' => $duration,
'providerName' => $this->getName(),
'result' => $result,
];
}
/**
* @return array
*/
public function getQueries(): array
{
return $this->queries;
}
public function __call($method, $args)
{
return call_user_func_array([$this->realProvider, $method], $args);
}
public function getName(): string
{
return $this->realProvider->getName();
}
}