ShowHolidayCommand::execute()   B
last analyzed

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