|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bazinga\Bundle\GeocoderBundle\DataCollector; |
| 4 | + |
| 5 | +use Bazinga\Bundle\GeocoderBundle\Logger\GeocoderLogger; |
| 6 | +use Geocoder\Collection; |
| 7 | +use Geocoder\Provider\Provider; |
| 8 | +use Geocoder\Query\GeocodeQuery; |
| 9 | +use Geocoder\Query\ReverseQuery; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
| 13 | + */ |
| 14 | +class ProfilingProvider implements Provider |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var Provider |
| 18 | + */ |
| 19 | + private $realProvider; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var GeocoderLogger |
| 23 | + */ |
| 24 | + private $logger; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param Provider $realProvider |
| 28 | + * @param GeocoderLogger $logger |
| 29 | + */ |
| 30 | + public function __construct(Provider $realProvider, GeocoderLogger $logger) |
| 31 | + { |
| 32 | + $this->realProvider = $realProvider; |
| 33 | + $this->logger = $logger; |
| 34 | + } |
| 35 | + |
| 36 | + public function geocodeQuery(GeocodeQuery $query): Collection |
| 37 | + { |
| 38 | + $startTime = microtime(true); |
| 39 | + try { |
| 40 | + $results = $this->realProvider->geocodeQuery($query); |
| 41 | + } finally { |
| 42 | + $duration = (microtime(true) - $startTime) * 1000; |
| 43 | + |
| 44 | + $this->logger->logRequest( |
| 45 | + sprintf('[Geocoding] %s', $query), |
| 46 | + $duration, |
| 47 | + $this->getName(), |
| 48 | + $results |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + return $results; |
| 53 | + } |
| 54 | + |
| 55 | + public function reverseQuery(ReverseQuery $query): Collection |
| 56 | + { |
| 57 | + $startTime = microtime(true); |
| 58 | + try { |
| 59 | + $results = $this->realProvider->reverseQuery($query); |
| 60 | + } finally { |
| 61 | + $duration = (microtime(true) - $startTime) * 1000; |
| 62 | + |
| 63 | + $this->logger->logRequest( |
| 64 | + sprintf('[Geocoding] %s', $query), |
| 65 | + $duration, |
| 66 | + $this->getName(), |
| 67 | + $results |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + return $results; |
| 72 | + } |
| 73 | + |
| 74 | + public function __call($method, $args) |
| 75 | + { |
| 76 | + return call_user_func_array([$this->realProvider, $method], $args); |
| 77 | + } |
| 78 | + |
| 79 | + public function getName(): string |
| 80 | + { |
| 81 | + return $this->realProvider->getName(); |
| 82 | + } |
| 83 | +} |
0 commit comments