Passed
Push — fix-assets-images-managment ( a7deda...445b39 )
by Arnaud
06:17 queued 39s
created

Clean::execute()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
nc 32
nop 2
dl 0
loc 44
rs 8.8497
c 0
b 0
f 0
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 Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputDefinition;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class Clean extends Command
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('clean')
27
            ->setDescription('Remove the output directory')
28
            ->setDefinition(
29
                new InputDefinition([
30
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
31
                ])
32
            )
33
            ->setHelp('Remove the output directory and temporary files.');
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $doSomething = false;
42
43
        // delete output dir
44
        $outputDir = (string) $this->getBuilder($output)->getConfig()->get('output.dir');
45
        if ($this->fs->exists($this->getPath().'/'.self::TMP_DIR.'/output')) {
46
            $outputDir = file_get_contents($this->getPath().'/'.self::TMP_DIR.'/output');
47
        }
48
        if ($this->fs->exists($this->getPath().'/'.$outputDir)) {
49
            $this->fs->remove($this->getPath().'/'.$outputDir);
50
            $output->writeln('Output directory removed.');
51
            $output->writeln(
52
                sprintf('-> %s', $this->getPath().'/'.$outputDir),
53
                OutputInterface::VERBOSITY_VERBOSE
54
            );
55
            $doSomething = true;
56
        }
57
        // delete local server temp files
58
        if ($this->fs->exists($this->getPath().'/'.self::TMP_DIR)) {
59
            $this->fs->remove($this->getPath().'/'.self::TMP_DIR);
60
            $output->writeln('Temporary directory deleted.');
61
            $output->writeln(
62
                sprintf('-> %s', $this->getPath().'/'.self::TMP_DIR),
63
                OutputInterface::VERBOSITY_VERBOSE
64
            );
65
            $doSomething = true;
66
        }
67
        // delete cache directory
68
        if ($this->fs->exists($this->builder->getConfig()->getCachePath())) {
69
            $this->fs->remove($this->builder->getConfig()->getCachePath());
70
            $output->writeln('Cache directory deleted.');
71
            $output->writeln(
72
                sprintf('-> %s', $this->builder->getConfig()->getCachePath()),
73
                OutputInterface::VERBOSITY_VERBOSE
74
            );
75
            $doSomething = true;
76
        }
77
78
        if ($doSomething === false) {
79
            $output->writeln('Nothing to do.');
80
        }
81
82
        return 0;
83
    }
84
}
85