Completed
Push — master ( 392de1...22df44 )
by Tobias
22:17
created

ProfilingProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 28.89 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 26
loc 90
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A geocodeQuery() 13 13 1
A reverseQuery() 13 13 1
A logQuery() 0 18 3
A getQueries() 0 4 1
A __call() 0 4 1
A getName() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bazinga\Bundle\GeocoderBundle\DataCollector;
4
5
use Geocoder\Collection;
6
use Geocoder\Exception\LogicException;
7
use Geocoder\Location;
8
use Geocoder\Provider\Provider;
9
use Geocoder\Query\GeocodeQuery;
10
use Geocoder\Query\ReverseQuery;
11
12
/**
13
 * @author Tobias Nyholm <[email protected]>
14
 */
15
class ProfilingProvider implements Provider
16
{
17
    /**
18
     * @var Provider
19
     */
20
    private $realProvider;
21
22
    /**
23
     * @var array
24
     */
25
    private $queries = [];
26
27
    /**
28
     * @param Provider       $realProvider
29
     */
30
    public function __construct(Provider $realProvider)
31
    {
32
        $this->realProvider = $realProvider;
33
    }
34
35 View Code Duplication
    public function geocodeQuery(GeocodeQuery $query): Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        $startTime = microtime(true);
38
        try {
39
            $result = $this->realProvider->geocodeQuery($query);
40
        } finally {
41
            $duration = (microtime(true) - $startTime) * 1000;
42
43
            $this->logQuery($query, $duration, $result);
44
        }
45
46
        return $result;
47
    }
48
49 View Code Duplication
    public function reverseQuery(ReverseQuery $query): Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $startTime = microtime(true);
52
        try {
53
            $result = $this->realProvider->reverseQuery($query);
54
        } finally {
55
            $duration = (microtime(true) - $startTime) * 1000;
56
57
            $this->logQuery($query, $duration, $result);
58
        }
59
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;
93
    }
94
95
    public function __call($method, $args)
96
    {
97
        return call_user_func_array([$this->realProvider, $method], $args);
98
    }
99
100
    public function getName(): string
101
    {
102
        return $this->realProvider->getName();
103
    }
104
}
105