ListCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 16
nc 1
nop 2
1
<?php
2
/**
3
 * CRM library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Crm\Command;
7
8
use Slince\Crm\Registry;
9
use Symfony\Component\Console\Helper\Table;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class ListCommand extends Command
14
{
15
    /**
16
     * Command name
17
     * @var string
18
     */
19
    const NAME = 'ls';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function configure()
25
    {
26
        parent::configure();
27
        $this->setName(static::NAME)
28
            ->setDescription("List all available registries");
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $currentRegistry = $this->getManager()->getCurrentRegistry($this->checkIsCurrent($input));
37
        $registries = $this->getManager()->getRegistries();
38
39
        //find all registry records
40
        $rows = array_map(function (Registry $registry) use ($currentRegistry) {
41
            if ($currentRegistry == $registry) {
42
                return [
43
                    '<info>*</info>',
44
                    "<info>{$registry->getName()}</info>",
45
                    "<info>{$registry->getUrl()}</info>"
46
                ];
47
            } else {
48
                return [
49
                    '',
50
                    $registry->getName(),
51
                    $registry->getUrl()
52
                ];
53
            }
54
        }, $registries->all());
55
        $this->outputResult($rows, $output);
56
    }
57
58
    /**
59
     * Output table
60
     * @param $rows
61
     * @param OutputInterface $output
62
     */
63
    protected function outputResult($rows, OutputInterface $output)
64
    {
65
        $table = new Table($output);
66
        $table->setRows($rows);
67
        $table->setStyle('compact');
68
        $table->render();
69
    }
70
}
71