Passed
Push — feature/clear-cache ( e2345a )
by Arnaud
07:42
created

Clear::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
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\Util;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputDefinition;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Removes generated and temporary files.
21
 */
22
class Clear extends AbstractCommand
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function configure()
28
    {
29
        $this
30
            ->setName('clear')
31
            ->setDescription('Removes generated files')
32
            ->setAliases(['clean'])
33
            ->setDefinition(
34
                new InputDefinition([
35
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
36
                ])
37
            )
38
            ->setHelp('Removes generated and temporary files');
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $this->removeOutputDir($output);
47
        $this->removeTmpDir($output);
48
49
        // deletes cache
50
        $command = $this->getApplication()->find('cache:clear');
51
        $returnCode = $command->run($input, $output);
0 ignored issues
show
Unused Code introduced by
The assignment to $returnCode is dead and can be removed.
Loading history...
52
53
        return 0;
54
    }
55
56
    /**
57
     * Deletes output directory
58
     *
59
     * @param OutputInterface $output
60
     */
61
    private function removeOutputDir(OutputInterface $output): void
62
    {
63
        $outputDir = (string) $this->getBuilder()->getConfig()->get('output.dir');
64
        if ($this->fs->exists(Util::joinFile($this->getPath(), self::TMP_DIR, 'output'))) {
65
            $outputDir = Util\File::fileGetContents(Util::joinFile($this->getPath(), self::TMP_DIR, 'output'));
66
        }
67
68
        if ($outputDir === false || !$this->fs->exists(Util::joinFile($this->getPath(), $outputDir))) {
69
            $output->writeln('<info>No output directory.</info>');
70
71
            return;
72
        }
73
74
        $output->writeln('Removing output directory...');
75
        $output->writeln(
76
            sprintf('<comment>Path: %s</comment>', Util::joinFile($this->getPath(), $outputDir)),
77
            OutputInterface::VERBOSITY_VERBOSE
78
        );
79
        $this->fs->remove(Util::joinFile($this->getPath(), $outputDir));
80
        $output->writeln('<info>Output directory is clear.</info>');
81
    }
82
83
    /**
84
     * Deletes local server temporary files
85
     *
86
     * @param OutputInterface $output
87
     */
88
    private function removeTmpDir(OutputInterface $output): void
89
    {
90
        if (!$this->fs->exists(Util::joinFile($this->getPath(), self::TMP_DIR))) {
91
            $output->writeln('<info>No temporary files.</info>');
92
93
            return;
94
        }
95
96
        $output->writeln('Removing temporary directory...');
97
        $output->writeln(
98
            sprintf('<comment>Path: %s</comment>', Util::joinFile($this->getPath(), self::TMP_DIR)),
99
            OutputInterface::VERBOSITY_VERBOSE
100
        );
101
        $this->fs->remove(Util::joinFile($this->getPath(), self::TMP_DIR));
102
        $output->writeln('<info>Temporary directory is clear.</info>');
103
    }
104
}
105