Completed
Push — fix-cmd-showcontent ( bbdb13 )
by Arnaud
01:59
created

ShowContent::getFilesTree()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/*
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[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 Cecil\Command;
12
13
use Cecil\Command\ShowContent\FileExtensionFilter;
14
use Cecil\Command\ShowContent\FilenameRecursiveTreeIterator;
15
use Cecil\Util\Plateform;
16
use RecursiveDirectoryIterator;
17
use RecursiveTreeIterator;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputDefinition;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
class ShowContent extends Command
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('show:content')
32
            ->setDescription('Show content')
33
            ->setDefinition(
34
                new InputDefinition([
35
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
36
                ])
37
            )
38
            ->setHelp('Show content as tree.');
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $contentDir = $this->getBuilder($output)->getConfig()->get('content.dir');
47
        $dataDir = $this->getBuilder($output)->getConfig()->get('data.dir');
48
49
        // format output
50
        $unicodeTreePrefix = function (RecursiveTreeIterator $tree) {
51
            $prefixParts = [
52
                RecursiveTreeIterator::PREFIX_LEFT         => ' ',
53
                RecursiveTreeIterator::PREFIX_MID_HAS_NEXT => '│ ',
54
                RecursiveTreeIterator::PREFIX_END_HAS_NEXT => '├ ',
55
                RecursiveTreeIterator::PREFIX_END_LAST     => '└ ',
56
            ];
57
            foreach ($prefixParts as $part => $string) {
58
                $tree->setPrefixPart($part, $string);
59
            }
60
        };
61
62
        try {
63
            // pages content
64
            $output->writeln(sprintf('<info>%s/</info>', $contentDir));
65
            $pages = $this->getFilesTree($output, $contentDir);
66
            if (!Plateform::isWindows()) {
67
                $unicodeTreePrefix($pages);
68
            }
69
            foreach ($pages as $page) {
70
                $output->writeln($page);
71
            }
72
            // data content
73
            if (is_dir($this->getPath().'/'.$dataDir)) {
74
                $output->writeln(sprintf('<info>%s/</info>', $dataDir));
75
                $datas = $this->getFilesTree($output, $dataDir);
76
                if (!Plateform::isWindows()) {
77
                    $unicodeTreePrefix($datas);
78
                }
79
                foreach ($datas as $data) {
80
                    $output->writeln($data);
81
                }
82
            }
83
        } catch (\Exception $e) {
84
            throw new \Exception(sprintf($e->getMessage()));
85
        }
86
87
        return 0;
88
    }
89
90
    /**
91
     * Return a console displayable tree of files.
92
     *
93
     * @param OutputInterface $output
94
     * @param string          $directory
95
     *
96
     * @throws \Exception
97
     *
98
     * @return FilenameRecursiveTreeIterator
99
     */
100
    public function getFilesTree(OutputInterface $output, string $directory)
101
    {
102
        $path = $this->getBuilder($output)->getConfig()->get("$directory.dir");
0 ignored issues
show
Unused Code introduced by
$path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103
        $ext = $this->getBuilder($output)->getConfig()->get("$directory.ext");
104
        $path = $this->getPath().'/'. $directory;
105
106
        if (!is_dir($path)) {
107
            throw new \Exception(sprintf('Invalid directory: %s.', $path));
108
        }
109
110
        $dirIterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
111
        $dirIterator = new FileExtensionFilter($dirIterator, $ext);
112
        $files = new FilenameRecursiveTreeIterator(
113
            $dirIterator,
114
            FilenameRecursiveTreeIterator::SELF_FIRST
115
        );
116
117
        return $files;
118
    }
119
}
120