Completed
Push — master ( 709dbb...f12a68 )
by Tobias
09:11
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
/*
4
 * This file is part of the BazingaGeocoderBundle package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @license    MIT License
9
 */
10
11
namespace Bazinga\GeocoderBundle\DataCollector;
12
13
use Geocoder\Collection;
14
use Geocoder\Exception\LogicException;
15
use Geocoder\Provider\Provider;
16
use Geocoder\Query\GeocodeQuery;
17
use Geocoder\Query\ReverseQuery;
18
19
/**
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
class ProfilingProvider implements Provider
23
{
24
    /**
25
     * @var Provider
26
     */
27
    private $realProvider;
28
29
    /**
30
     * @var array
31
     */
32
    private $queries = [];
33
34
    /**
35
     * @param Provider $realProvider
36
     */
37
    public function __construct(Provider $realProvider)
38
    {
39
        $this->realProvider = $realProvider;
40
    }
41
42 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...
43
    {
44
        $startTime = microtime(true);
45
        try {
46
            $result = $this->realProvider->geocodeQuery($query);
47
        } finally {
48
            $duration = (microtime(true) - $startTime) * 1000;
49
50
            $this->logQuery($query, $duration, $result);
51
        }
52
53
        return $result;
54
    }
55
56 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...
57
    {
58
        $startTime = microtime(true);
59
        try {
60
            $result = $this->realProvider->reverseQuery($query);
61
        } finally {
62
            $duration = (microtime(true) - $startTime) * 1000;
63
64
            $this->logQuery($query, $duration, $result);
65
        }
66
67
        return $result;
68
    }
69
70
    /**
71
     * @param GeocodeQuery|ReverseQuery $query
72
     * @param float                     $duration geocoding duration
73
     * @param Collection                $result
74
     */
75
    private function logQuery($query, float $duration, Collection $result = null)
76
    {
77
        if ($query instanceof GeocodeQuery) {
78
            $queryString = $query->getText();
79
        } elseif ($query instanceof ReverseQuery) {
80
            $queryString = sprintf('(%s, %s)', $query->getCoordinates()->getLongitude(), $query->getCoordinates()->getLatitude());
81
        } else {
82
            throw new LogicException('First parameter to ProfilingProvider::logQuery must be a query');
83
        }
84
85
        $this->queries[] = [
86
            'query' => $query,
87
            'queryString' => $queryString,
88
            'duration' => $duration,
89
            'providerName' => $this->getName(),
90
            'result' => $result,
91
        ];
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public function getQueries(): array
98
    {
99
        return $this->queries;
100
    }
101
102
    public function __call($method, $args)
103
    {
104
        return call_user_func_array([$this->realProvider, $method], $args);
105
    }
106
107
    public function getName(): string
108
    {
109
        return $this->realProvider->getName();
110
    }
111
}
112