Clear   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 73
ccs 0
cts 42
cp 0
rs 10
c 2
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A removeOutputDir() 0 18 4
A removeTmpDir() 0 13 2
A configure() 0 10 1
A execute() 0 9 1
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
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 Cecil\Command;
15
16
use Cecil\Util;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * Clear command.
23
 *
24
 * This command removes all generated files, including the output directory, temporary directory, and cache files.
25
 * It is useful for cleaning up the build environment before starting a new build or to free up space.
26
 */
27
class Clear extends AbstractCommand
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('clear')
36
            ->setDescription('Removes all generated files')
37
            ->setDefinition([
38
                new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
39
            ])
40
            ->setHelp(
41
                <<<'EOF'
42
The <info>%command.name%</> command removes output directory, temporary directory and cache files.
43
EOF
44
            );
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output): int
51
    {
52
        $this->removeOutputDir($output);
53
        $this->removeTmpDir($output);
54
        // deletes all cache files
55
        $command = $this->getApplication()->find('cache:clear');
56
        $command->run($input, $output);
57
58
        return 0;
59
    }
60
61
    /**
62
     * Removes the output directory.
63
     */
64
    private function removeOutputDir(OutputInterface $output): void
65
    {
66
        $outputDir = (string) $this->getBuilder()->getConfig()->get('output.dir');
67
        // if custom output directory
68
        if (Util\File::getFS()->exists(Util::joinFile($this->getPath(), self::TMP_DIR, 'output'))) {
69
            $outputDir = Util\File::fileGetContents(Util::joinFile($this->getPath(), self::TMP_DIR, 'output'));
70
        }
71
        if ($outputDir === false || !Util\File::getFS()->exists(Util::joinFile($this->getPath(), $outputDir))) {
72
            $output->writeln('<info>No output directory.</info>');
73
            return;
74
        }
75
        $output->writeln('Removing output directory...');
76
        $output->writeln(
77
            \sprintf('<comment>Path: %s</comment>', Util::joinFile($this->getPath(), $outputDir)),
78
            OutputInterface::VERBOSITY_VERBOSE
79
        );
80
        Util\File::getFS()->remove(Util::joinFile($this->getPath(), $outputDir));
81
        $output->writeln('<info>Output directory removed.</info>');
82
    }
83
84
    /**
85
     * Removes temporary directory.
86
     */
87
    private function removeTmpDir(OutputInterface $output): void
88
    {
89
        if (!Util\File::getFS()->exists(Util::joinFile($this->getPath(), self::TMP_DIR))) {
90
            $output->writeln('<info>No temporary directory.</info>');
91
            return;
92
        }
93
        $output->writeln('Removing temporary directory...');
94
        $output->writeln(
95
            \sprintf('<comment>Path: %s</comment>', Util::joinFile($this->getPath(), self::TMP_DIR)),
96
            OutputInterface::VERBOSITY_VERBOSE
97
        );
98
        Util\File::getFS()->remove(Util::joinFile($this->getPath(), self::TMP_DIR));
99
        $output->writeln('<info>Temporary directory removed.</info>');
100
    }
101
}
102