Passed
Push — develop ( 8e88eb...1cf98b )
by Brent
02:42
created

RouterListCommand::execute()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 12
nop 2
dl 0
loc 32
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Command;
4
5
use Brendt\Stitcher\Stitcher;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class RouterListCommand extends Command
12
{
13
14
    const FILTER = 'filter';
15
16
    protected function configure() {
17
        $this->setName('router:list')
18
            ->setDescription('Show the available routes')
19
            ->setHelp("This command shows the available routes.")
20
            ->addArgument(self::FILTER, InputArgument::OPTIONAL, 'Specify a filter');
21
    }
22
23
    /**
24
     * @param InputInterface  $input
25
     * @param OutputInterface $output
26
     *
27
     * @return void
28
     */
29
    protected function execute(InputInterface $input, OutputInterface $output) {
30
        $stitcher = Stitcher::create();
31
        $site = $stitcher->loadSite();
32
        $filter = $input->getArgument(self::FILTER);
33
34
        if ($filter) {
35
            $output->writeln("Available routes (filtered by <fg=green>{$filter}</>):\n");
36
        } else {
37
            $output->writeln("Available routes:\n");
38
        }
39
40
        foreach ($site as $route => $page) {
41
            if ($filter && strpos($route, $filter) === false) {
42
                continue;
43
            }
44
45
            $data = [];
46
47
            if (isset($page['data'])) {
48
                $data = array_keys($page['data']);
49
            }
50
51
            $line = "- <fg=green>{$route}</>: {$page['template']}.tpl";
52
53
            if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
54
                $line .= "\n\t";
55
                $line .= '$' . implode(', $', $data);
56
            }
57
58
            $output->writeln($line);
59
        }
60
    }
61
62
}
63