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 CacheInterface |
||
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 | 61 | public function __construct(ProviderAggregator $geocoder) |
|
56 | { |
||
57 | 61 | $this->geocoder = $geocoder; |
|
58 | 61 | } |
|
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) |
|
69 | { |
||
70 | 34 | return isset($this->cache) ? $this->cache->isCached($providerName, $query) : false; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Cache the BatchGeocoded object. |
||
75 | * |
||
76 | * @param BatchGeocoded $geocoded The BatchGeocoded object to cache. |
||
77 | * |
||
78 | * @return BatchGeocoded The BatchGeocoded object. |
||
79 | */ |
||
80 | 9 | public function cache(BatchGeocoded $geocoded) |
|
88 | |||
89 | /** |
||
90 | * {@inheritDoc} |
||
91 | */ |
||
92 | 28 | public function geocode($values) |
|
93 | { |
||
94 | 28 | $geocoder = $this->geocoder; |
|
95 | 28 | $cache = $this; |
|
96 | |||
97 | 28 | foreach ($geocoder->getProviders() as $provider) { |
|
98 | 28 | if (is_array($values) && count($values) > 0) { |
|
99 | 9 | foreach ($values as $value) { |
|
100 | 9 | $this->tasks[] = function () use ($geocoder, $provider, $value, $cache) { |
|
101 | 8 | $deferred = new Deferred; |
|
102 | |||
103 | try { |
||
104 | 8 | if ($cached = $cache->isCached($provider->getName(), $value)) { |
|
105 | 4 | $deferred->resolve($cached); |
|
106 | } else { |
||
107 | 4 | $batchResult = new BatchResult($provider->getName(), $value); |
|
108 | 4 | $address = $geocoder->using($provider->getName())->geocode($value)->first(); |
|
109 | 6 | $deferred->resolve($cache->cache($batchResult->createFromAddress($address))); |
|
110 | } |
||
111 | 2 | } catch (\Exception $e) { |
|
112 | 2 | $batchGeocoded = new BatchResult($provider->getName(), $value, $e->getMessage()); |
|
113 | 2 | $deferred->reject($batchGeocoded->newInstance()); |
|
114 | } |
||
115 | |||
116 | 8 | return $deferred->promise(); |
|
117 | 9 | }; |
|
118 | } |
||
119 | 19 | } elseif (is_string($values) && '' !== trim($values)) { |
|
120 | 12 | $this->tasks[] = function () use ($geocoder, $provider, $values, $cache) { |
|
121 | 8 | $deferred = new Deferred; |
|
122 | |||
123 | try { |
||
124 | 8 | if ($cached = $cache->isCached($provider->getName(), $values)) { |
|
125 | 4 | $deferred->resolve($cached); |
|
126 | } else { |
||
127 | 4 | $batchResult = new BatchResult($provider->getName(), $values); |
|
128 | 4 | $address = $geocoder->using($provider->getName())->geocode($values)->first(); |
|
129 | 6 | $deferred->resolve($cache->cache($batchResult->createFromAddress($address))); |
|
130 | } |
||
131 | 2 | } catch (\Exception $e) { |
|
132 | 2 | $batchGeocoded = new BatchResult($provider->getName(), $values, $e->getMessage()); |
|
133 | 2 | $deferred->reject($batchGeocoded->newInstance()); |
|
134 | } |
||
135 | |||
136 | 8 | return $deferred->promise(); |
|
137 | 12 | }; |
|
138 | } else { |
||
139 | 7 | throw new InvalidArgumentException( |
|
140 | 28 | 'The argument should be a string or an array of strings to geocode.' |
|
141 | ); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | 21 | return $this; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritDoc} |
||
150 | */ |
||
151 | 26 | public function reverse($coordinates) |
|
215 | |||
216 | /** |
||
217 | * {@inheritDoc} |
||
218 | * |
||
219 | * $this cannot be used in anonymous function in PHP 5.3.x |
||
220 | * @see http://php.net/manual/en/functions.anonymous.php |
||
221 | */ |
||
222 | 17 | public function serie() |
|
223 | { |
||
224 | 17 | $computedInSerie = array(); |
|
225 | |||
226 | 17 | foreach ($this->tasks as $task) { |
|
227 | 13 | $task()->then(function($result) use (&$computedInSerie) { |
|
228 | 13 | $computedInSerie[] = $result; |
|
229 | }, function ($emptyResult) use (&$computedInSerie) { |
||
230 | 4 | $computedInSerie[] = $emptyResult; |
|
231 | 17 | }); |
|
232 | } |
||
233 | |||
234 | 16 | return $computedInSerie; |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * {@inheritDoc} |
||
239 | * |
||
240 | * $this cannot be used in anonymous function in PHP 5.3.x |
||
241 | * @see http://php.net/manual/en/functions.anonymous.php |
||
242 | */ |
||
243 | 17 | public function parallel() |
|
262 | |||
263 | /** |
||
264 | * {@inheritDoc} |
||
265 | */ |
||
266 | 19 | public function setCache(CacheInterface $cache) |
|
272 | } |
||
273 |