1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bazinga\Bundle\GeocoderBundle\DataCollector; |
4
|
|
|
|
5
|
|
|
use Geocoder\Collection; |
6
|
|
|
use Geocoder\Exception\LogicException; |
7
|
|
|
use Geocoder\Location; |
8
|
|
|
use Geocoder\Provider\Provider; |
9
|
|
|
use Geocoder\Query\GeocodeQuery; |
10
|
|
|
use Geocoder\Query\ReverseQuery; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Tobias Nyholm <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class ProfilingProvider implements Provider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Provider |
19
|
|
|
*/ |
20
|
|
|
private $realProvider; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $queries = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Provider $realProvider |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Provider $realProvider) |
31
|
|
|
{ |
32
|
|
|
$this->realProvider = $realProvider; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public function geocodeQuery(GeocodeQuery $query): Collection |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$startTime = microtime(true); |
38
|
|
|
try { |
39
|
|
|
$result = $this->realProvider->geocodeQuery($query); |
40
|
|
|
} finally { |
41
|
|
|
$duration = (microtime(true) - $startTime) * 1000; |
42
|
|
|
|
43
|
|
|
$this->logQuery($query, $duration, $result); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
public function reverseQuery(ReverseQuery $query): Collection |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$startTime = microtime(true); |
52
|
|
|
try { |
53
|
|
|
$result = $this->realProvider->reverseQuery($query); |
54
|
|
|
} finally { |
55
|
|
|
$duration = (microtime(true) - $startTime) * 1000; |
56
|
|
|
|
57
|
|
|
$this->logQuery($query, $duration, $result); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param GeocodeQuery|ReverseQuery $query |
65
|
|
|
* @param float $duration geocoding duration |
66
|
|
|
* @param Collection $result |
67
|
|
|
*/ |
68
|
|
|
private function logQuery($query, float $duration, Collection $result = null) |
69
|
|
|
{ |
70
|
|
|
if ($query instanceof GeocodeQuery) { |
71
|
|
|
$queryString = $query->getText(); |
72
|
|
|
} elseif ($query instanceof ReverseQuery) { |
73
|
|
|
$queryString = sprintf('(%s, %s)', $query->getCoordinates()->getLongitude(), $query->getCoordinates()->getLongitude()); |
74
|
|
|
} else { |
75
|
|
|
throw new LogicException('First parameter to ProfilingProvider::logQuery must be a query'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->queries[] = array( |
79
|
|
|
'query' => $query, |
80
|
|
|
'queryString' => $queryString, |
81
|
|
|
'duration' => $duration, |
82
|
|
|
'providerName' => $this->getName(), |
83
|
|
|
'result' => $result, |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getQueries(): array |
91
|
|
|
{ |
92
|
|
|
return $this->queries; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function __call($method, $args) |
96
|
|
|
{ |
97
|
|
|
return call_user_func_array([$this->realProvider, $method], $args); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getName(): string |
101
|
|
|
{ |
102
|
|
|
return $this->realProvider->getName(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.