Test Failed
Push — master ( a58c94...1bcec0 )
by Brent
03:26
created

CleanCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Command;
4
5
use Brendt\Stitcher\Config;
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
use Symfony\Component\Filesystem\Filesystem;
11
use Symfony\Component\Console\Question\Question;
12
use Symfony\Component\Finder\Finder;
13
14
class CleanCommand extends Command {
15
16
    const FORCE = 'force';
17
18
    protected function configure() {
19
        $this->setName('site:clean')
20
            ->setDescription('Do a cleanup')
21
            ->setHelp("This command cleans all generated files.")
22
            ->addOption(self::FORCE, InputArgument::OPTIONAL);
23
    }
24
25
    /**
26
     * @param InputInterface  $input
27
     * @param OutputInterface $output
28
     *
29
     * @return int|null|void
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output) {
32
        Config::load();
33
34
        $force = $input->getOption(self::FORCE);
35
        $publicDir = Config::get('directories.public');
36
        $cacheDir = Config::get('directories.cache');
37
38
        if (!$force) {
39
            $questionHelper = $this->getHelper('question');
40
            $question = new Question("Are you sure you want to clean all generated files ({$publicDir} and {$cacheDir}) [y/N] ", false);
41
42
            if (!$questionHelper->ask($input, $output, $question)) {
43
                return;
44
            }
45
        }
46
47
        $fs = new Filesystem();
48
        $finder = new Finder();
49
        $log = [];
50
51
        if ($fs->exists($publicDir)) {
52
            $publicDirectories = $finder->directories()->in($publicDir);
53
            $directoryPaths = [];
54
            foreach ($publicDirectories as $directory) {
55
                $directoryPaths[] = $directory->getPathname();
56
            }
57
            $fs->remove($directoryPaths);
58
59
            $publicFiles = $finder->files()->in($publicDir)->notName('.htaccess')->ignoreDotFiles(true);
60
            foreach ($publicFiles as $file) {
61
                $fs->remove($file->getPathname());
62
            }
63
64
            $log[] = "Cleaned the public directory: {$publicDir}";
65
        }
66
67
        if ($fs->exists($cacheDir)) {
68
            $fs->remove($cacheDir);
69
70
            $log[] = "Removed the cache directory: {$cacheDir}";
71
        }
72
73 View Code Duplication
        if (count($log)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            $output->writeln("Successfully cleaned up\n");
75
76
            foreach ($log as $line) {
77
                $output->writeln("- {$line}");
78
            }
79
80
            $output->writeln("\nRun <fg=green>site:generate</> to generate these files again.");
81
        } else {
82
            $output->writeln('No files were found');
83
        }
84
85
86
        return;
87
88
    }
89
}
90