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\Plugin; |
14
|
|
|
|
15
|
|
|
use Geocoder\Collection; |
16
|
|
|
use Geocoder\Exception\LogicException; |
17
|
|
|
use Geocoder\Plugin\Plugin; |
18
|
|
|
use Geocoder\Query\GeocodeQuery; |
19
|
|
|
use Geocoder\Query\Query; |
20
|
|
|
use Geocoder\Query\ReverseQuery; |
21
|
|
|
use Geocoder\Exception\Exception; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Tobias Nyholm <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ProfilingPlugin implements Plugin |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $queries = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string service id of the provider; |
35
|
|
|
*/ |
36
|
|
|
private $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $name |
40
|
|
|
*/ |
41
|
|
|
public function __construct(string $name) |
42
|
|
|
{ |
43
|
|
|
$this->name = $name; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function handleQuery(Query $query, callable $next, callable $first) |
47
|
|
|
{ |
48
|
|
|
$startTime = microtime(true); |
49
|
|
|
|
50
|
|
|
return $next($query)->then(function (Collection $result) use ($query, $startTime) { |
51
|
|
|
$duration = (microtime(true) - $startTime) * 1000; |
52
|
|
|
$this->logQuery($query, $duration, $result); |
53
|
|
|
|
54
|
|
|
return $result; |
55
|
|
|
}, function (Exception $exception) use ($query, $startTime) { |
56
|
|
|
$duration = (microtime(true) - $startTime) * 1000; |
57
|
|
|
$this->logQuery($query, $duration, $exception); |
58
|
|
|
|
59
|
|
|
throw $exception; |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Query $query |
65
|
|
|
* @param float $duration geocoding duration |
66
|
|
|
* @param Collection|Exception $result |
67
|
|
|
*/ |
68
|
|
|
private function logQuery(Query $query, float $duration, $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()->getLatitude()); |
74
|
|
|
} else { |
75
|
|
|
throw new LogicException('First parameter to ProfilingProvider::logQuery must be a Query'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->queries[] = [ |
79
|
|
|
'query' => $query, |
80
|
|
|
'queryString' => $queryString, |
81
|
|
|
'duration' => $duration, |
82
|
|
|
'providerName' => $this->getName(), |
83
|
|
|
'result' => $result, |
84
|
|
|
'resultCount' => $result instanceof Collection ? $result->count() : 0, |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function getQueries(): array |
92
|
|
|
{ |
93
|
|
|
return $this->queries; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getName(): string |
100
|
|
|
{ |
101
|
|
|
return $this->name; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|