Passed
Push — develop ( e6f4d5...d3a65c )
by Brent
06:41
created

RouterListCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Command;
4
5
use Brendt\Stitcher\Site\Page;
6
use Brendt\Stitcher\Stitcher;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class RouterListCommand extends Command
13
{
14
    const FILTER = 'filter';
15
16
    /**
17
     * @var Stitcher
18
     */
19
    private $stitcher;
20
21
    public function __construct(string $configPath = './config.yml', array $defaultConfig = []) {
22
        parent::__construct();
23
24
        $this->stitcher = Stitcher::create($configPath, $defaultConfig);
25
    }
26
27
    protected function configure() {
28
        $this->setName('router:list')
29
            ->setDescription('Show the available routes')
30
            ->setHelp("This command shows the available routes.")
31
            ->addArgument(self::FILTER, InputArgument::OPTIONAL, 'Specify a filter');
32
    }
33
34
    /**
35
     * @param InputInterface  $input
36
     * @param OutputInterface $output
37
     *
38
     * @return void
39
     */
40
    protected function execute(InputInterface $input, OutputInterface $output) {
41
        $site = $this->stitcher->loadSite();
42
        $filter = $input->getArgument(self::FILTER);
43
44
        $output->writeln("Found routes:");
45
46
        /**
47
         * @var string $route
48
         * @var Page $page
49
         */
50
        foreach ($site as $route => $page) {
51
            if ($filter && strpos($page->getId(), $filter) === false) {
52
                continue;
53
            }
54
55
            $output->writeln("- <fg=green>{$page->getId()}</>: {$page->getTemplatePath()}.tpl");
56
57
            foreach ($page->getVariables() as $variable => $value) {
58
                $output->writeln("\t\${$variable}: {$value}");
59
            }
60
        }
61
    }
62
}
63