1 | <?php |
||
27 | class Batch implements BatchInterface |
||
28 | { |
||
29 | /** |
||
30 | * The Geocoder instance to use. |
||
31 | * |
||
32 | * @var ProviderAggregator |
||
33 | */ |
||
34 | protected $geocoder; |
||
35 | |||
36 | /** |
||
37 | * An array of closures. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $tasks; |
||
42 | |||
43 | /** |
||
44 | * The cache instance to use. |
||
45 | * |
||
46 | * @var CacheItemPoolInterface |
||
47 | */ |
||
48 | protected $cache; |
||
49 | |||
50 | /** |
||
51 | * Set the Geocoder instance to use. |
||
52 | * |
||
53 | * @param ProviderAggregator $geocoder The Geocoder instance to use. |
||
54 | */ |
||
55 | 60 | public function __construct(ProviderAggregator $geocoder) |
|
59 | |||
60 | /** |
||
61 | * Check against the cache instance if any. |
||
62 | * |
||
63 | * @param string $providerName The name of the provider. |
||
64 | * @param string $query The query string. |
||
65 | * |
||
66 | * @return boolean|BatchGeocoded The BatchGeocoded object from the query or the cache instance. |
||
67 | */ |
||
68 | 34 | public function isCached($providerName, $query) |
|
82 | |||
83 | |||
84 | /** |
||
85 | * Cache the BatchGeocoded object. |
||
86 | * |
||
87 | * @param BatchGeocoded $geocoded The BatchGeocoded object to cache. |
||
88 | * |
||
89 | * @return BatchGeocoded The BatchGeocoded object. |
||
90 | */ |
||
91 | 9 | public function cache(BatchGeocoded $geocoded) |
|
102 | |||
103 | /** |
||
104 | * {@inheritDoc} |
||
105 | */ |
||
106 | 28 | public function geocode($values) |
|
107 | { |
||
108 | 28 | $geocoder = $this->geocoder; |
|
109 | 28 | $cache = $this; |
|
110 | |||
111 | 28 | foreach ($geocoder->getProviders() as $provider) { |
|
112 | 28 | if (is_array($values) && count($values) > 0) { |
|
113 | 9 | foreach ($values as $value) { |
|
114 | 9 | $this->tasks[] = function () use ($geocoder, $provider, $value, $cache) { |
|
115 | 8 | $deferred = new Deferred; |
|
116 | |||
117 | try { |
||
118 | 8 | if ($cached = $cache->isCached($provider->getName(), $value)) { |
|
119 | 4 | $deferred->resolve($cached); |
|
120 | } else { |
||
121 | 4 | $batchResult = new BatchResult($provider->getName(), $value); |
|
122 | 4 | $address = $geocoder->using($provider->getName())->geocode($value)->first(); |
|
123 | 6 | $deferred->resolve($cache->cache($batchResult->createFromAddress($address))); |
|
124 | } |
||
125 | 2 | } catch (\Exception $e) { |
|
126 | 2 | $batchGeocoded = new BatchResult($provider->getName(), $value, $e->getMessage()); |
|
127 | 2 | $deferred->reject($batchGeocoded->newInstance()); |
|
128 | } |
||
129 | |||
130 | 8 | return $deferred->promise(); |
|
131 | 9 | }; |
|
132 | } |
||
133 | 19 | } elseif (is_string($values) && '' !== trim($values)) { |
|
134 | 12 | $this->tasks[] = function () use ($geocoder, $provider, $values, $cache) { |
|
135 | 8 | $deferred = new Deferred; |
|
136 | |||
137 | try { |
||
138 | 8 | if ($cached = $cache->isCached($provider->getName(), $values)) { |
|
139 | 4 | $deferred->resolve($cached); |
|
140 | } else { |
||
141 | 4 | $batchResult = new BatchResult($provider->getName(), $values); |
|
142 | 4 | $address = $geocoder->using($provider->getName())->geocode($values)->first(); |
|
143 | 6 | $deferred->resolve($cache->cache($batchResult->createFromAddress($address))); |
|
144 | } |
||
145 | 2 | } catch (\Exception $e) { |
|
146 | 2 | $batchGeocoded = new BatchResult($provider->getName(), $values, $e->getMessage()); |
|
147 | 2 | $deferred->reject($batchGeocoded->newInstance()); |
|
148 | } |
||
149 | |||
150 | 8 | return $deferred->promise(); |
|
151 | 12 | }; |
|
152 | } else { |
||
153 | 7 | throw new InvalidArgumentException( |
|
154 | 28 | 'The argument should be a string or an array of strings to geocode.' |
|
155 | ); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | 21 | return $this; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * {@inheritDoc} |
||
164 | */ |
||
165 | 26 | public function reverse($coordinates) |
|
229 | |||
230 | /** |
||
231 | * {@inheritDoc} |
||
232 | * |
||
233 | * $this cannot be used in anonymous function in PHP 5.3.x |
||
234 | * @see http://php.net/manual/en/functions.anonymous.php |
||
235 | */ |
||
236 | 17 | public function serie() |
|
250 | |||
251 | /** |
||
252 | * {@inheritDoc} |
||
253 | * |
||
254 | * $this cannot be used in anonymous function in PHP 5.3.x |
||
255 | * @see http://php.net/manual/en/functions.anonymous.php |
||
256 | */ |
||
257 | 17 | public function parallel() |
|
276 | |||
277 | /** |
||
278 | * {@inheritDoc} |
||
279 | */ |
||
280 | 19 | public function setCache(CacheItemPoolInterface $cache) |
|
286 | |||
287 | 18 | private function getCacheKey(string $providerName, string $query): string |
|
291 | } |
||
292 |