ProfilingPlugin   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 10%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 67
ccs 3
cts 30
cp 0.1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handleQuery() 0 16 1
A logQuery() 0 19 4
A getQueries() 0 4 1
A getName() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the BazingaGeocoderBundle 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 Bazinga\GeocoderBundle\Plugin;
14
15
use Geocoder\Collection;
16
use Geocoder\Exception\Exception;
17
use Geocoder\Exception\LogicException;
18
use Geocoder\Plugin\Plugin;
19
use Geocoder\Query\GeocodeQuery;
20
use Geocoder\Query\Query;
21
use Geocoder\Query\ReverseQuery;
22
23
/**
24
 * @author Tobias Nyholm <[email protected]>
25
 */
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 1
    public function __construct(string $name)
39
    {
40 1
        $this->name = $name;
41 1
    }
42
43
    public function handleQuery(Query $query, callable $next, callable $first)
44
    {
45
        $startTime = microtime(true);
46
47
        return $next($query)->then(function (Collection $result) use ($query, $startTime) {
48
            $duration = (microtime(true) - $startTime) * 1000;
49
            $this->logQuery($query, $duration, $result);
50
51
            return $result;
52
        }, function (Exception $exception) use ($query, $startTime) {
53
            $duration = (microtime(true) - $startTime) * 1000;
54
            $this->logQuery($query, $duration, $exception);
55
56
            throw $exception;
57
        });
58
    }
59
60
    /**
61
     * @param Collection|Exception $result
62
     */
63
    private function logQuery(Query $query, float $duration, $result = null)
64
    {
65
        if ($query instanceof GeocodeQuery) {
66
            $queryString = $query->getText();
67
        } elseif ($query instanceof ReverseQuery) {
68
            $queryString = sprintf('(%s, %s)', $query->getCoordinates()->getLongitude(), $query->getCoordinates()->getLatitude());
69
        } else {
70
            throw new LogicException('First parameter to ProfilingProvider::logQuery must be a Query');
71
        }
72
73
        $this->queries[] = [
74
            'query' => $query,
75
            'queryString' => $queryString,
76
            'duration' => $duration,
77
            'providerName' => $this->getName(),
78
            'result' => $result,
79
            'resultCount' => $result instanceof Collection ? $result->count() : 0,
80
        ];
81
    }
82
83
    public function getQueries(): array
84
    {
85
        return $this->queries;
86
    }
87
88
    public function getName(): string
89
    {
90
        return $this->name;
91
    }
92
}
93