Completed
Push — master ( a7de57...5589b2 )
by Tobias
05:33
created

GeocodeCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the BazingaGeocoderBundle package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Bazinga\GeocoderBundle\Command;
14
15
use Geocoder\ProviderAggregator;
16
use Geocoder\Query\GeocodeQuery;
17
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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
 * @author Markus Bachmann <[email protected]>
25
 */
26
class GeocodeCommand extends ContainerAwareCommand
27
{
28
    protected static $defaultName = 'geocoder:geocode';
29
30
    /**
31
     * @var ProviderAggregator
32
     */
33
    private $geocoder;
34
35
    /**
36
     * @param ProviderAggregator $geocoder
37
     */
38 1
    public function __construct(ProviderAggregator $geocoder)
39
    {
40 1
        $this->geocoder = $geocoder;
41
42 1
        parent::__construct();
43 1
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    protected function configure()
49
    {
50
        $this
51 1
            ->setName('geocoder:geocode')
52 1
            ->setDescription('Geocode an address or a ip address')
53 1
            ->addArgument('address', InputArgument::REQUIRED, 'The address')
54 1
            ->addOption('provider', null, InputOption::VALUE_OPTIONAL)
55 1
            ->setHelp(<<<'HELP'
56 1
The <info>geocoder:geocoder</info> command will fetch the latitude
57
and longitude from the given address.
58
59
You can force a provider with the "provider" option.
60
61
<info>php bin/console geocoder:geocoder "Eiffel Tower" --provider=yahoo</info>
62
HELP
63
            );
64 1
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    protected function execute(InputInterface $input, OutputInterface $output)
70
    {
71 1
        if ($input->getOption('provider')) {
72
            $this->geocoder->using($input->getOption('provider'));
73
        }
74
75 1
        $results = $this->geocoder->geocodeQuery(GeocodeQuery::create($input->getArgument('address')));
76 1
        $data = $results->first()->toArray();
77
78 1
        $max = 0;
79
80 1
        foreach ($data as $key => $value) {
81 1
            $length = strlen($key);
82 1
            if ($max < $length) {
83 1
                $max = $length;
84
            }
85
        }
86
87 1
        $max += 2;
88
89 1
        foreach ($data as $key => $value) {
90 1
            $key = $this->humanize($key);
91
92 1
            $output->writeln(sprintf(
93 1
                '<comment>%s</comment>: %s',
94 1
                str_pad($key, $max, ' ', STR_PAD_RIGHT),
95 1
                is_array($value) ? json_encode($value) : $value
96
            ));
97
        }
98 1
    }
99
100 1
    private function humanize(string $text): string
101
    {
102 1
        $text = preg_replace('/([A-Z][a-z]+)|([A-Z][A-Z]+)|([^A-Za-z ]+)/', ' \1', $text);
103
104 1
        return ucfirst(strtolower($text));
105
    }
106
}
107