22
33namespace Bazinga \Bundle \GeocoderBundle \DataCollector ;
44
5- use Bazinga \Bundle \GeocoderBundle \Logger \GeocoderLogger ;
65use Geocoder \Collection ;
6+ use Geocoder \Exception \LogicException ;
7+ use Geocoder \Location ;
78use Geocoder \Provider \Provider ;
89use Geocoder \Query \GeocodeQuery ;
910use Geocoder \Query \ReverseQuery ;
@@ -19,56 +20,76 @@ class ProfilingProvider implements Provider
1920 private $ realProvider ;
2021
2122 /**
22- * @var GeocoderLogger
23+ * @var array
2324 */
24- private $ logger ;
25+ private $ queries = [] ;
2526
2627 /**
2728 * @param Provider $realProvider
28- * @param GeocoderLogger $logger
2929 */
30- public function __construct (Provider $ realProvider, GeocoderLogger $ logger )
30+ public function __construct (Provider $ realProvider )
3131 {
3232 $ this ->realProvider = $ realProvider ;
33- $ this ->logger = $ logger ;
3433 }
3534
3635 public function geocodeQuery (GeocodeQuery $ query ): Collection
3736 {
3837 $ startTime = microtime (true );
3938 try {
40- $ results = $ this ->realProvider ->geocodeQuery ($ query );
39+ $ result = $ this ->realProvider ->geocodeQuery ($ query );
4140 } finally {
4241 $ duration = (microtime (true ) - $ startTime ) * 1000 ;
4342
44- $ this ->logger ->logRequest (
45- sprintf ('[Geocoding] %s ' , $ query ),
46- $ duration ,
47- $ this ->getName (),
48- $ results
49- );
43+ $ this ->logQuery ($ query , $ duration , $ result );
5044 }
5145
52- return $ results ;
46+ return $ result ;
5347 }
5448
5549 public function reverseQuery (ReverseQuery $ query ): Collection
5650 {
5751 $ startTime = microtime (true );
5852 try {
59- $ results = $ this ->realProvider ->reverseQuery ($ query );
53+ $ result = $ this ->realProvider ->reverseQuery ($ query );
6054 } finally {
6155 $ duration = (microtime (true ) - $ startTime ) * 1000 ;
6256
63- $ this ->logger ->logRequest (
64- sprintf ('[Geocoding] %s ' , $ query ),
65- $ duration ,
66- $ this ->getName (),
67- $ results
68- );
57+ $ this ->logQuery ($ query , $ duration , $ result );
6958 }
7059
71- return $ results ;
60+ return $ result ;
61+ }
62+
63+ /**
64+ * @param GeocodeQuery|ReverseQuery $query
65+ * @param float $duration geocoding duration
66+ * @param Collection $result
67+ */
68+ private function logQuery ($ query , float $ duration , Collection $ 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 ()->getLongitude ());
74+ } else {
75+ throw new LogicException ('First parameter to ProfilingProvider::logQuery must be a query ' );
76+ }
77+
78+ $ this ->queries [] = array (
79+ 'query ' => $ query ,
80+ 'queryString ' => $ queryString ,
81+ 'duration ' => $ duration ,
82+ 'providerName ' => $ this ->getName (),
83+ 'result ' => $ result ,
84+ );
85+ }
86+
87+ /**
88+ * @return array
89+ */
90+ public function getQueries (): array
91+ {
92+ return $ this ->queries ;
7293 }
7394
7495 public function __call ($ method , $ args )
0 commit comments