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

Reverse   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 13

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 2
cbo 13
dl 0
loc 107
ccs 25
cts 75
cp 0.3333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 35 1
C execute() 0 68 15
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\Formatter\StringFormatter;
15
use Geocoder\ProviderAggregator;
16
use League\Geotools\Batch\Batch;
17
use League\Geotools\Coordinate\Coordinate;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * Command-line geocoder:reverse class
25
 *
26
 * @author Antoine Corcy <[email protected]>
27
 */
28
class Reverse extends Command
29
{
30 2
    protected function configure()
31
    {
32
        $this
33 2
            ->setName('geocoder:reverse')
34 2
            ->setDescription('Reverse geocode street address, IPv4 or IPv6 against a provider with an adapter')
35 2
            ->addArgument('coordinate', InputArgument::REQUIRED, 'The coordinate to reverse')
36 2
            ->addOption('provider', null, InputOption::VALUE_REQUIRED,
37 2
                'If set, the name of the provider to use, Google Maps by default', 'google_maps')
38 2
            ->addOption('adapter', null, InputOption::VALUE_REQUIRED,
39 2
                'If set, the name of the adapter to use, cURL by default', 'curl')
40 2
            ->addOption('cache', null, InputOption::VALUE_REQUIRED,
41 2
                'If set, the name of the cache to use, Redis by default')
42 2
            ->addOption('raw', null, InputOption::VALUE_NONE,
43 2
                'If set, the raw format of the reverse geocoding result')
44 2
            ->addOption('json', null, InputOption::VALUE_NONE,
45 2
                'If set, the json format of the reverse geocoding result')
46 2
            ->addOption('args', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
47 2
                'If set, the provider constructor arguments like api key, locale, region, ssl, toponym and service')
48 2
            ->addOption('format', null, InputOption::VALUE_REQUIRED,
49 2
                'If set, the format of the reverse geocoding result', '%S %n, %z %L')
50 2
            ->setHelp(<<<EOT
51 2
<info>Available adapters</info>:   {$this->getAdapters()}
52 2
<info>Available providers</info>:  {$this->getProviders()} <comment>(some providers need arguments)</comment>
53 2
<info>Available dumpers</info>:    {$this->getDumpers()}
54
55
<info>Use the default provider with the socket adapter and format the output</info>:
56
57
    %command.full_name% "48.8631507, 2.388911" <comment>--format="%L, %R, %C" --adapter=socket</comment>
58
59
<info>Use the OpenStreetMaps provider with the default adapter</info>:
60
61
    %command.full_name% "48.8631507, 2.388911" <comment>--provider=openstreetmaps</comment>
62
EOT
63
            );
64 2
    }
65
66 1
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68 1
        $coordinate = new Coordinate($input->getArgument('coordinate'));
69
70
        $geocoder = new ProviderAggregator;
71
        $adapter  = $this->getAdapter($input->getOption('adapter'));
72
        $provider = $this->getProvider($input->getOption('provider'));
73
74
        if ($input->getOption('args')) {
75
            $args = is_array($input->getOption('args'))
76
                ? implode(',', $input->getOption('args'))
77
                : $input->getOption('args');
78
            $geocoder->registerProvider(new $provider(new $adapter(), $args));
79
        } else {
80
            $geocoder->registerProvider(new $provider(new $adapter()));
81
        }
82
83
        $batch = new Batch($geocoder);
84
        if ($input->getOption('cache')) {
85
            $batch->setCache( $this->getCache($input->getOption('cache')));
86
        }
87
88
        $reversed = $batch->reverse($coordinate)->parallel();
89
        $address = $reversed[0]->first();
90
91
        if ($input->getOption('raw')) {
92
            $result = array();
93
            $result[] = sprintf('<label>Adapter</label>:       <value>%s</value>', $adapter);
94
            $result[] = sprintf('<label>Provider</label>:      <value>%s</value>', $provider);
95
            $result[] = sprintf('<label>Cache</label>:         <value>%s</value>', isset($cache) ? $cache : 'None');
0 ignored issues
show
Bug introduced by
The variable $cache seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
96
            if ($input->getOption('args')) {
97
                $result[] = sprintf('<label>Arguments</label>:     <value>%s</value>', $args);
98
            }
99
            $result[] = '---';
100
            $coordinates = $address->getCoordinates();
101
            $result[] = sprintf('<label>Latitude</label>:      <value>%s</value>', null !== $coordinates ? $coordinates->getLatitude() : '');
102
            $result[] = sprintf('<label>Longitude</label>:     <value>%s</value>', null !== $coordinates ? $coordinates->getLongitude() : '');
103
            if ($address->getBounds()) {
104
                $bounds = $address->getBounds()->toArray();
105
                $result[] = '<label>Bounds</label>';
106
                $result[] = sprintf(' - <label>South</label>: <value>%s</value>', $bounds['south']);
107
                $result[] = sprintf(' - <label>West</label>:  <value>%s</value>', $bounds['west']);
108
                $result[] = sprintf(' - <label>North</label>: <value>%s</value>', $bounds['north']);
109
                $result[] = sprintf(' - <label>East</label>:  <value>%s</value>', $bounds['east']);
110
            }
111
            $result[] = sprintf('<label>Street Number</label>: <value>%s</value>', $address->getStreetNumber());
112
            $result[] = sprintf('<label>Street Name</label>:   <value>%s</value>', $address->getStreetName());
113
            $result[] = sprintf('<label>Zipcode</label>:       <value>%s</value>', $address->getPostalCode());
114
            $result[] = sprintf('<label>City</label>:          <value>%s</value>', $address->getLocality());
115
            $result[] = sprintf('<label>City District</label>: <value>%s</value>', $address->getSubLocality());
116
            if ( NULL !== $adminLevels = $address->getAdminLevels() ) {
117
                $result[] = '<label>Admin Levels</label>';
118
                foreach ($adminLevels as $adminLevel) {
119
                    $result[] = sprintf(' - <label>%s</label>: <value>%s</value>', $adminLevel->getCode(), $adminLevel->getName());
120
                }
121
            }
122
            $country = $address->getCountry();
123
            $result[] = sprintf('<label>Country</label>:       <value>%s</value>', null !== $country ? $country->getName() : '');
124
            $result[] = sprintf('<label>Country Code</label>:  <value>%s</value>', null !== $country ? $country->getCode() : '');
125
            $result[] = sprintf('<label>Timezone</label>:      <value>%s</value>', $address->getTimezone());
126
        } elseif ($input->getOption('json')) {
127
            $result = sprintf('<value>%s</value>', json_encode($address->toArray()));
128
        } else {
129
            $result = (new StringFormatter)->format($address, $input->getOption('format'));
130
        }
131
132
        $output->writeln($result);
133
    }
134
}
135