1 | <?php |
||
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 | 1 | public function __construct(string $name) |
|
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) |
||
87 | |||
88 | /** |
||
89 | * @return array |
||
90 | */ |
||
91 | public function getQueries(): array |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getName(): string |
||
103 | } |
||
104 |