Completed
Push — template-events/cli-debug ( 4c2253 )
by Kamil
04:51
created

DebugTemplateEventCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\UiBundle\Command;
15
16
use Sylius\Bundle\UiBundle\Registry\TemplateBlock;
17
use Sylius\Bundle\UiBundle\Registry\TemplateBlockRegistryInterface;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Style\SymfonyStyle;
23
24
final class DebugTemplateEventCommand extends Command
25
{
26
    /** @var TemplateBlockRegistryInterface */
27
    private $templateBlockRegistry;
28
29
    public function __construct(TemplateBlockRegistryInterface $templateBlockRegistry)
30
    {
31
        parent::__construct();
32
33
        $this->templateBlockRegistry = $templateBlockRegistry;
34
    }
35
36
    protected function configure(): void
37
    {
38
        $this
39
            ->setDescription('Debug template events and associated blocks')
40
            ->addArgument('event', InputArgument::OPTIONAL, 'Template event name', null)
41
        ;
42
    }
43
44
    protected function execute(InputInterface $input, OutputInterface $output): int
45
    {
46
        $io = new SymfonyStyle($input, $output);
47
        $eventName = $input->getArgument('event');
48
49
        if ($eventName) {
50
            $io->title(sprintf('Blocks registered for the template event "%s"', $eventName));
51
52
            $io->table(
53
                ['Block name', 'Template', 'Priority', 'Enabled'],
54
                array_map(
55
                    static function (TemplateBlock $templateBlock): array {
56
                        return [
57
                            $templateBlock->name(),
58
                            $templateBlock->template(),
59
                            $templateBlock->priority(),
60
                            $templateBlock->enabled() ? 'TRUE' : 'FALSE',
61
                        ];
62
                    },
63
                    $this->templateBlockRegistry->all()[$eventName] ?? []
64
                )
65
            );
66
67
            return 0;
68
        }
69
70
        $io->title('List of template events');
71
72
        $io->listing(array_keys($this->templateBlockRegistry->all()));
73
74
        return 0;
75
    }
76
}
77