Completed
Push — master ( 511dbe...f8a0cd )
by Tobias
05:55
created

Command::getDumpers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Geotools library.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\Geotools\CLI\Command\Geocoder;
13
14
use Psr\Cache\CacheItemPoolInterface;
15
16
/**
17
 * Command class
18
 *
19
 * @author Antoine Corcy <[email protected]>
20
 */
21
class Command extends \Symfony\Component\Console\Command\Command
22
{
23
    /**
24
     * Available adapters.
25
     *
26
     * @var array
27
     */
28
    private $adapters = [
29
        'buzz'    => 'BuzzHttpAdapter',
30
        'curl'    => 'CurlHttpAdapter',
31
        'guzzle'  => 'GuzzleHttpAdapter',
32
        'socket'  => 'SocketHttpAdapter',
33
        'zend'    => 'ZendHttpAdapter',
34
    ];
35
36
    /**
37
     * Available providers.
38
     *
39
     * @var array
40
     */
41
    private $providers = [
42
        'free_geo_ip'          => 'FreeGeoIp',
43
        'host_ip'              => 'HostIp',
44
        'ip_info_db'           => 'IpInfoDb',
45
        'google_maps'          => 'GoogleMaps',
46
        'google_maps_business' => 'GoogleMapsBusiness',
47
        'bing_maps'            => 'BingMaps',
48
        'openstreetmaps'       => 'OpenStreetMap',
49
        'cloudmade'            => 'CloudMade',
50
        'geoip'                => 'Geoip',
51
        'map_quest'            => 'MapQuest',
52
        'oio_rest'             => 'OIORest',
53
        'geocoder_ca'          => 'GeocoderCa',
54
        'geocoder_us'          => 'GeocoderUs',
55
        'ign_openls'           => 'IGNOpenLS',
56
        'data_science_toolkit' => 'DataScienceToolkit',
57
        'yandex'               => 'Yandex',
58
        'geo_plugin'           => 'GeoPlugin',
59
        'geo_ips'              => 'GeoIPs',
60
        'maxmind'              => 'MaxMind',
61
        'geonames'             => 'Geonames',
62
        'ip_geo_base'          => 'IpGeoBase',
63
        'baidu'                => 'Baidu',
64
        'tomtom'               => 'TomTom',
65
        'arcgis_online'        => 'ArcGISOnline',
66
    ];
67
68
    /**
69
     * Available dumpers.
70
     *
71
     * @var array
72
     */
73
    private $dumpers = [
74
        'gpx'     => 'Gpx',
75
        'geojson' => 'GeoJson',
76
        'kml'     => 'Kml',
77
        'wkb'     => 'Wkb',
78
        'wkt'     => 'Wkt',
79
    ];
80
81
82
    /**
83
     * @param  string $factoryCallable
84
     *
85
     * @return CacheItemPoolInterface
86
     */
87
    protected function getCache($factoryCallable)
88
    {
89
        $factoryCallable = $this->lowerize((trim($factoryCallable)));
90
        if (!is_callable($factoryCallable)) {
91
            throw new \LogicException(sprintf('Cache must be called with a valid callable on the format "Example\Acme::create". "%s" given.', $factoryCallable));
92
        }
93
94
        $psr6 = call_user_func($factoryCallable);
95
        if (!$psr6 instanceof CacheItemPoolInterface) {
96
            throw new \LogicException('Return value of factory callable must be a CacheItemPoolInterface');
97
        }
98
99
        return $psr6;
100
    }
101
102
    /**
103
     * Returns the adapter class name.
104
     * The default adapter is cURL.
105
     *
106
     * @param string $adapter The name of the adapter to use.
107
     *
108
     * @return string The name of the adapter class to use.
109
     */
110
    protected function getAdapter($adapter)
111
    {
112
        $adapter = $this->lowerize((trim($adapter)));
113
        $adapter = array_key_exists($adapter, $this->adapters)
114
            ? $this->adapters[$adapter]
115
            : $this->adapters['curl'];
116
117
        return '\\Ivory\\HttpAdapter\\' . $adapter;
118
    }
119
120
    /**
121
     * Returns the list of available adapters sorted by alphabetical order.
122
     *
123
     * @return string The list of available adapters comma separated.
124
     */
125 3
    protected function getAdapters()
126
    {
127 3
        ksort($this->adapters);
128
129 3
        return implode(', ', array_keys($this->adapters));
130
    }
131
132
    /**
133
     * Returns the provider class name.
134
     * The default provider is Google Maps.
135
     *
136
     * @param string $provider The name of the provider to use.
137
     *
138
     * @return string The name of the provider class to use.
139
     */
140
    protected function getProvider($provider)
141
    {
142
        $provider = $this->lowerize((trim($provider)));
143
        $provider = array_key_exists($provider, $this->providers)
144
            ? $this->providers[$provider]
145
            : $this->providers['google_maps'];
146
147
        return '\\Geocoder\\Provider\\' . $provider;
148
    }
149
150
    /**
151
     * Returns the list of available providers sorted by alphabetical order.
152
     *
153
     * @return string The list of available providers comma separated.
154
     */
155 3
    protected function getProviders()
156
    {
157 3
        ksort($this->providers);
158
159 3
        return implode(', ', array_keys($this->providers));
160
    }
161
162
    /**
163
     * Returns the dumper class name.
164
     * The default dumper is WktDumper.
165
     *
166
     * @param string $dumper The name of the dumper to use.
167
     *
168
     * @return string The name of the dumper class to use.
169
     */
170
    protected function getDumper($dumper)
171
    {
172
        $dumper = $this->lowerize((trim($dumper)));
173
        $dumper = array_key_exists($dumper, $this->dumpers)
174
            ? $this->dumpers[$dumper]
175
            : $this->dumpers['wkt'];
176
177
        return '\\Geocoder\\Dumper\\' . $dumper;
178
    }
179
180
    /**
181
     * Returns the list of available dumpers sorted by alphabetical order.
182
     *
183
     * @return string The list of available dumpers comma separated.
184
     */
185 3
    protected function getDumpers()
186
    {
187 3
        ksort($this->dumpers);
188
189 3
        return implode(', ', array_keys($this->dumpers));
190
    }
191
192
    /**
193
     * Make a string lowercase.
194
     *
195
     * @param string $string A string to lowercase.
196
     *
197
     * @return string The lowercased string.
198
     */
199
    private function lowerize($string)
200
    {
201
        return extension_loaded('mbstring') ? mb_strtolower($string, 'UTF-8') : strtolower($string);
202
    }
203
}
204