TimedGeocoder.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Geocoder package.
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @license MIT License
  9. */
  10. namespace Geocoder;
  11. use Geocoder\Query\GeocodeQuery;
  12. use Geocoder\Query\ReverseQuery;
  13. use Geocoder\Provider\Provider;
  14. use Symfony\Component\Stopwatch\Stopwatch;
  15. /**
  16. * This Geocoder allows you to profile your API/Database calls.
  17. *
  18. * @author Markus Bachmann <markus.bachmann@bachi.biz>
  19. */
  20. final class TimedGeocoder implements Geocoder
  21. {
  22. use GeocoderTrait;
  23. /**
  24. * @var Provider
  25. */
  26. private $delegate;
  27. /**
  28. * @var Stopwatch
  29. */
  30. private $stopwatch;
  31. public function __construct(Provider $delegate, Stopwatch $stopwatch)
  32. {
  33. $this->delegate = $delegate;
  34. $this->stopwatch = $stopwatch;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function geocodeQuery(GeocodeQuery $query): Collection
  40. {
  41. $this->stopwatch->start('geocode', 'geocoder');
  42. try {
  43. $result = $this->delegate->geocodeQuery($query);
  44. } catch (\Throwable $e) {
  45. $this->stopwatch->stop('geocode');
  46. throw $e;
  47. }
  48. $this->stopwatch->stop('geocode');
  49. return $result;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function reverseQuery(ReverseQuery $query): Collection
  55. {
  56. $this->stopwatch->start('reverse', 'geocoder');
  57. try {
  58. $result = $this->delegate->reverseQuery($query);
  59. } catch (\Throwable $e) {
  60. $this->stopwatch->stop('reverse');
  61. throw $e;
  62. }
  63. $this->stopwatch->stop('reverse');
  64. return $result;
  65. }
  66. public function __call($method, $args)
  67. {
  68. return call_user_func_array([$this->delegate, $method], $args);
  69. }
  70. public function getName(): string
  71. {
  72. return 'timed_geocoder';
  73. }
  74. }