Code

< 40 %
40-60 %
> 60 %
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder 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 Geocoder;
14
15
use Geocoder\Provider\Provider;
16
use Geocoder\Query\GeocodeQuery;
17
use Geocoder\Query\ReverseQuery;
18
use Symfony\Component\Stopwatch\Stopwatch;
19
20
/**
21
 * This Geocoder allows you to profile your API/Database calls.
22
 *
23
 * @author Markus Bachmann <[email protected]>
24
 */
25
final class TimedGeocoder implements Geocoder
26
{
27
    use GeocoderTrait;
28
29
    /**
30
     * @var Provider
31
     */
32
    private $delegate;
33
34
    /**
35
     * @var Stopwatch
36
     */
37
    private $stopwatch;
38
39 4
    public function __construct(Provider $delegate, Stopwatch $stopwatch)
40
    {
41 4
        $this->delegate = $delegate;
42 4
        $this->stopwatch = $stopwatch;
43
    }
44
45 2
    public function geocodeQuery(GeocodeQuery $query): Collection
46
    {
47 2
        $this->stopwatch->start('geocode', 'geocoder');
48
49
        try {
50 2
            $result = $this->delegate->geocodeQuery($query);
51 1
        } catch (\Throwable $e) {
52 1
            $this->stopwatch->stop('geocode');
53
54 1
            throw $e;
55
        }
56
57 1
        $this->stopwatch->stop('geocode');
58
59 1
        return $result;
60
    }
61
62 2
    public function reverseQuery(ReverseQuery $query): Collection
63
    {
64 2
        $this->stopwatch->start('reverse', 'geocoder');
65
66
        try {
67 2
            $result = $this->delegate->reverseQuery($query);
68 1
        } catch (\Throwable $e) {
69 1
            $this->stopwatch->stop('reverse');
70
71 1
            throw $e;
72
        }
73
74 1
        $this->stopwatch->stop('reverse');
75
76 1
        return $result;
77
    }
78
79
    public function __call($method, $args)
80
    {
81
        return call_user_func_array([$this->delegate, $method], $args);
82
    }
83
84
    public function getName(): string
85
    {
86
        return 'timed_geocoder';
87
    }
88
}
89