Completed
Push — master ( 660335...0e6c98 )
by Tobias
03:05
created

StatefulGeocoder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 142
Duplicated Lines 14.08 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 20
loc 142
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A geocode() 0 15 3
B geocodeQuery() 0 14 5
A setLocale() 0 6 1
A setBounds() 0 6 1
A setLimit() 0 6 1
A getName() 0 4 1
A reverse() 11 11 2
A reverseQuery() 9 9 3

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder 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 Geocoder;
14
15
use Geocoder\Model\Bounds;
16
use Geocoder\Query\GeocodeQuery;
17
use Geocoder\Query\ReverseQuery;
18
use Geocoder\Provider\Provider;
19
20
/**
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
final class StatefulGeocoder implements Geocoder
24
{
25
    /**
26
     * @var string
27
     */
28
    private $locale;
29
30
    /**
31
     * @var Bounds
32
     */
33
    private $bounds;
34
35
    /**
36
     * @var int
37
     */
38
    private $limit;
39
40
    /**
41
     * @var Provider
42
     */
43
    private $provider;
44
45
    /**
46
     * @param Provider $provider
47
     * @param string   $locale
48
     */
49
    public function __construct(Provider $provider, string $locale = null)
50
    {
51
        $this->provider = $provider;
52
        $this->locale = $locale;
53
        $this->limit = Geocoder::DEFAULT_RESULT_LIMIT;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function geocode(string $value): Collection
60
    {
61
        $query = GeocodeQuery::create($value)
62
            ->withLimit($this->limit);
63
64
        if (!empty($this->locale)) {
65
            $query = $query->withLocale($this->locale);
66
        }
67
68
        if (!empty($this->bounds)) {
69
            $query = $query->withBounds($this->bounds);
70
        }
71
72
        return $this->provider->geocodeQuery($query);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 View Code Duplication
    public function reverse(float $latitude, float $longitude): 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...
79
    {
80
        $query = ReverseQuery::fromCoordinates($latitude, $longitude)
81
            ->withLimit($this->limit);
82
83
        if (!empty($this->locale)) {
84
            $query = $query->withLocale($this->locale);
85
        }
86
87
        return $this->provider->reverseQuery($query);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function geocodeQuery(GeocodeQuery $query): Collection
94
    {
95
        $locale = $query->getLocale();
96
        if (empty($locale) && null !== $this->locale) {
97
            $query = $query->withLocale($this->locale);
98
        }
99
100
        $bounds = $query->getBounds();
101
        if (empty($bounds) && null !== $this->bounds) {
102
            $query = $query->withBounds($this->bounds);
103
        }
104
105
        return $this->provider->geocodeQuery($query);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 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...
112
    {
113
        $locale = $query->getLocale();
114
        if (empty($locale) && null !== $this->locale) {
115
            $query = $query->withLocale($this->locale);
116
        }
117
118
        return $this->provider->reverseQuery($query);
119
    }
120
121
    /**
122
     * @param string $locale
123
     *
124
     * @return StatefulGeocoder
125
     */
126
    public function setLocale(string $locale): self
127
    {
128
        $this->locale = $locale;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param Bounds $bounds
135
     *
136
     * @return StatefulGeocoder
137
     */
138
    public function setBounds(Bounds $bounds): self
139
    {
140
        $this->bounds = $bounds;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param int $limit
147
     *
148
     * @return StatefulGeocoder
149
     */
150
    public function setLimit(int $limit): self
151
    {
152
        $this->limit = $limit;
153
154
        return $this;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function getName(): string
161
    {
162
        return 'stateful_geocoder';
163
    }
164
}
165