Completed
Push — master ( 8fae0f...b69ce8 )
by Taosikai
14:32
created

ShowRegionCommand::execute()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 5
nop 2
1
<?php
2
/*
3
 * This file is part of the Slince/China package.
4
 *
5
 * (c) Slince <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace China\Command\Dashboard;
11
12
use China\Holiday\HolidayInterface;
13
use China\Region\Location\AddressInterface;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ChoiceQuestion;
18
use Symfony\Component\Console\Style\SymfonyStyle;
19
20
class ShowRegionCommand extends DashboardCommand
21
{
22
    protected static $types = [
23
        AddressInterface::TYPE_PROVINCE => '省',
24
        AddressInterface::TYPE_CITY => '市',
25
        AddressInterface::TYPE_AREA => '区 ',
26
    ];
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function configure()
31
    {
32
        $this->setName('dashboard:region');
33
        $this->addOption('parent', 'p', InputOption::VALUE_OPTIONAL);
34
        $this->setDescription('展示我国省市县信息');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $regionService = $this->getChina()->getRegion();
43
        $style = new SymfonyStyle($input, $output);
44
        $headers = ['Code', '名称', '类型'];
45
46
        if ($parentName = $input->getOption('parent')) {
47
            $parent = $regionService->findByName($parentName) ?: $regionService->findByCode($parentName);
48
            if ($parent === false) {
49
                throw new \InvalidArgumentException(sprintf('你提供的上级地区“%s”似乎是不存在的', $parentName));
50
            }
51
            $regions = $parent->getChildren()->toArray();
52
        } else {
53
            $regions = $regionService->getProvinces()->toArray();
54
        }
55
56
        $rows = array_map(function(AddressInterface $address){
57
            return [
58
                $address->getCode(),
59
                "<info>{$address->getName()}</info>",
60
                static::$types[$address->getType()],
61
            ];
62
        }, $regions);
63
        $style->table($headers, $rows);
64
    }
65
}