Completed
Push — master ( ef8e35...28395c )
by Antoine
05:19
created

Geocode::execute()   C

Complexity

Conditions 17
Paths 72

Size

Total Lines 75
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 0
cts 59
cp 0
rs 5.2934
c 0
b 0
f 0
cc 17
eloc 60
nc 72
nop 2
crap 306

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Geocoder\ProviderAggregator;
15
use League\Geotools\Batch\Batch;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * Command-line geocoder:geocode class
23
 *
24
 * @author Antoine Corcy <[email protected]>
25
 */
26
class Geocode extends Command
27
{
28 1
    protected function configure()
29
    {
30
        $this
31 1
            ->setName('geocoder:geocode')
32 1
            ->setDescription('Geocode a street-address, IPv4 or IPv6 against a provider with an adapter')
33 1
            ->addArgument('value', InputArgument::REQUIRED, 'The street-address, IPv4 or IPv6 to geocode')
34 1
            ->addOption('provider', null, InputOption::VALUE_REQUIRED,
35 1
                'If set, the name of the provider to use, Google Maps by default', 'google_maps')
36 1
            ->addOption('adapter', null, InputOption::VALUE_REQUIRED,
37 1
                'If set, the name of the adapter to use, cURL by default', 'curl')
38 1
            ->addOption('cache', null, InputOption::VALUE_REQUIRED,
39 1
                'If set, the name of the cache to use, Redis by default')
40 1
            ->addOption('raw', null, InputOption::VALUE_NONE,
41 1
                'If set, the raw format of the reverse geocoding result')
42 1
            ->addOption('json', null, InputOption::VALUE_NONE,
43 1
                'If set, the json format of the reverse geocoding result')
44 1
            ->addOption('dumper', null, InputOption::VALUE_REQUIRED,
45 1
                'If set, the name of the dumper to use, no dumper by default')
46 1
            ->addOption('args', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
47 1
                'If set, the provider constructor arguments like api key, locale, region, ssl, toponym and service')
48 1
            ->setHelp(<<<EOT
49 1
<info>Available adapters</info>:   {$this->getAdapters()}
50 1
<info>Available providers</info>:  {$this->getProviders()} <comment>(some providers need arguments)</comment>
51 1
<info>Available dumpers</info>:    {$this->getDumpers()}
52 1
<info>Available caches</info>:     {$this->getCaches()}
53
54
<info>Use the default provider with the socket adapter and dump the output in WKT standard</info>:
55
56
    %command.full_name% paris <comment>--adapter=socket --dumper=wkt</comment>
57
58
<info>Use the OpenStreetMaps provider with the default adapter</info>:
59
60
    %command.full_name% paris <comment>--provider=openstreetmaps</comment>
61
62
<info>Use the FreeGeoIp provider with the socket adapter</info>
63
64
    %command.full_name% 74.200.247.59 <comment>--provider="free_geo_ip" --adapter="socket"</comment>
65
66
<info>Use the default provider with the french locale and region via SSL</info>:
67
68 1
    %command.full_name% "Tagensvej 47, Copenhagen" <comment>--args=da_DK --args=Denmark --args="true"</comment>
69
EOT
70
            );
71 1
    }
72
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        $geocoder = new ProviderAggregator;
76
        $adapter  = $this->getAdapter($input->getOption('adapter'));
77
        $provider = $this->getProvider($input->getOption('provider'));
78
79
        if ($input->getOption('args')) {
80
            $args = is_array($input->getOption('args'))
81
                ? implode(',', $input->getOption('args'))
82
                : $input->getOption('args');
83
            $geocoder->registerProvider(new $provider(new $adapter(), $args));
84
        } else {
85
            $geocoder->registerProvider(new $provider(new $adapter()));
86
        }
87
88
        $batch = new Batch($geocoder);
89
        if ($input->getOption('cache')) {
90
            $cache = $this->getCache($input->getOption('cache'));
91
            $batch->setCache(new $cache());
92
        }
93
94
        $geocoded = $batch->geocode($input->getArgument('value'))->parallel();
95
        $address = $geocoded[0]->first();
96
97
        if ($input->getOption('raw')) {
98
            $result = array();
99
            $result[] = sprintf('<label>Adapter</label>:       <value>%s</value>', $adapter);
100
            $result[] = sprintf('<label>Provider</label>:      <value>%s</value>', $provider);
101
            $result[] = sprintf('<label>Cache</label>:         <value>%s</value>', isset($cache) ? $cache : 'None');
102
            if ($input->getOption('args')) {
103
                $result[] = sprintf('<label>Arguments</label>:     <value>%s</value>', $args);
104
            }
105
            $result[] = '---';
106
            $coordinates = $address->getCoordinates();
107
            $result[] = sprintf('<label>Latitude</label>:      <value>%s</value>', null !== $coordinates ? $coordinates->getLatitude() : '');
108
            $result[] = sprintf('<label>Longitude</label>:     <value>%s</value>', null !== $coordinates ? $coordinates->getLongitude() : '');
109
            if ($address->getBounds()) {
110
                $bounds = $address->getBounds()->toArray();
111
                $result[] = '<label>Bounds</label>';
112
                $result[] = sprintf(' - <label>South</label>: <value>%s</value>', $bounds['south']);
113
                $result[] = sprintf(' - <label>West</label>:  <value>%s</value>', $bounds['west']);
114
                $result[] = sprintf(' - <label>North</label>: <value>%s</value>', $bounds['north']);
115
                $result[] = sprintf(' - <label>East</label>:  <value>%s</value>', $bounds['east']);
116
            }
117
            $result[] = sprintf('<label>Street Number</label>: <value>%s</value>', $address->getStreetNumber());
118
            $result[] = sprintf('<label>Street Name</label>:   <value>%s</value>', $address->getStreetName());
119
            $result[] = sprintf('<label>Zipcode</label>:       <value>%s</value>', $address->getPostalCode());
120
            $result[] = sprintf('<label>City</label>:          <value>%s</value>', $address->getLocality());
121
            $result[] = sprintf('<label>City District</label>: <value>%s</value>', $address->getSublocality());
122
            if (null !== $adminLevels = $address->getAdminLevels()) {
123
                $result[] = '<label>Admin Levels</label>';
124
                foreach ($adminLevels as $adminLevel) {
125
                    $result[] = sprintf(' - <label>%s</label>: <value>%s</value>', $adminLevel->getCode(), $adminLevel->getName());
126
                }
127
            }
128
            $country = $address->getCountry();
129
            $result[] = sprintf('<label>Country</label>:       <value>%s</value>', null !== $country ? $country->getName() : '');
130
            $result[] = sprintf('<label>Country Code</label>:  <value>%s</value>', null !== $country ? $country->getCode() : '');
131
            $result[] = sprintf('<label>Timezone</label>:      <value>%s</value>', $address->getTimezone());
132
        } elseif ($input->getOption('json')) {
133
            $result = sprintf('<value>%s</value>', json_encode($address->toArray()));
134
        } elseif ($input->getOption('dumper')) {
135
            $dumper = $this->getDumper($input->getOption('dumper'));
136
            $dumper = new $dumper;
137
            $result = sprintf('<value>%s</value>', $dumper->dump($address));
138
        } else {
139
            $coordinates = $address->getCoordinates();
140
            $result = '<value>null, null</value>';
141
            if (null !== $coordinates) {
142
                $result = sprintf('<value>%s, %s</value>', $coordinates->getLatitude(), $coordinates->getLongitude());
143
            }
144
        }
145
146
        $output->writeln($result);
147
    }
148
}
149