Completed
Push — master ( c4a119...b5db7c )
by Tobias
01:22
created

StatefulGeocoder.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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