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

ShowHolidayCommand::execute()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 25
nc 3
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 Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Question\ChoiceQuestion;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
19
class ShowHolidayCommand extends DashboardCommand
20
{
21
    protected static $types = [
22
        HolidayInterface::TYPE_TRADITIONAL => '传统节日',
23
        HolidayInterface::TYPE_INTERNATIONAL => '国际节日',
24
        HolidayInterface::TYPE_SOLAR_TERM => '24节气',
25
    ];
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function configure()
30
    {
31
        $this->setName('dashboard:holiday');
32
        $this->addOption('type', 't', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '按照类型筛选');
33
        $this->addOption('all', 'a',  InputOption::VALUE_NONE, '展现全部数据');
34
        $this->setDescription('展示节假日信息');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $holidayService = $this->getChina()->getHoliday();
43
        $style = new SymfonyStyle($input, $output);
44
        $headers = ['名称', '类型', '日期'];
45
46
        if ($input->getOption('all')) {
47
            $holidays = $holidayService->findAll()->toArray();
48
        } else {
49
            $types = $input->getOption('type');
50
            if (empty($types)) {
51
                $question = new ChoiceQuestion('请选择节日类型', static::$types, HolidayInterface::TYPE_TRADITIONAL);
52
                $question->setMultiselect(true);
53
                $helper = $this->getHelper('question');
54
                $types = $helper->ask($input, $output, $question);
55
            } else {
56
                $this->checkTypes($types);
57
            }
58
            $holidays = [];
59
            foreach ($types as $type) {
60
                $holidays = array_merge($holidays, $holidayService->findHolidaysByType($type)->toArray());
61
            }
62
        }
63
        $rows = array_map(function(HolidayInterface $holiday){
64
            return [
65
                "<info>{$holiday->getName()}</info>",
66
                static::$types[$holiday->getType()],
67
                $holiday->getDate()
68
            ];
69
        }, $holidays);
70
        $style->table($headers, $rows);
71
    }
72
73
    protected function checkTypes($types)
74
    {
75
        foreach ($types as $type) {
76
            if (!isset(static::$types[$type])) {
77
                throw new \InvalidArgumentException(sprintf('类型 "%s" 不支持,请从(%s)做出选择',
78
                    $type,
79
                    implode(', ', array_keys(static::$types))
80
                ));
81
            }
82
        }
83
    }
84
}