Passed
Push — master ( c922f8...2df1f2 )
by Jonas
02:07
created

SetupPathCommand::execute()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 24
rs 9.5222
c 0
b 0
f 0
cc 5
nc 4
nop 2
1
<?php
2
3
namespace Glamorous\Boiler;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class SetupPathCommand extends ConfigurationCommand
10
{
11
    /**
12
     * Input to get arguments from.
13
     *
14
     * @var InputInterface
15
     */
16
    private $input;
17
18
    /**
19
     * Configure the command options.
20
     *
21
     * @return void
22
     */
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('setup')
27
            ->setDescription('Set the directory as a place to search for boiler-templates.')
28
            ->addArgument('directory', InputArgument::OPTIONAL);
29
    }
30
31
    /**
32
     * Execute the command.
33
     *
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     *
37
     * @return void
38
     * @throws BoilerException
39
     */
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $this->input = $input;
43
44
        $directory = $this->getDirectory();
45
        $path = realpath($directory);
46
47
        if ($path === false || !file_exists($path)) {
48
            throw new BoilerException('Directory does not exists');
49
        }
50
51
        $paths = $this->configuration->getPaths();
52
        if (in_array($path, $paths)) {
53
            throw new BoilerException('Folder already added.');
54
        }
55
        $result = $this->configuration->addPath($path);
56
57
        $resultMessage = '<comment>Directory successfully added</comment>';
58
59
        if (!$result) {
60
            $resultMessage = '<error>Directory could not be added.</error>';
61
        }
62
63
        $output->writeln($resultMessage);
64
    }
65
66
    /**
67
     * Get the directory given (or current one).
68
     *
69
     * @return string
70
     *
71
     * @throws BoilerException
72
     */
73
    protected function getDirectory()
74
    {
75
        $directory = $this->input->getArgument('directory');
76
77
        if (is_array($directory)) {
78
            throw new BoilerException('Only one directory is allowed.');
79
        }
80
81
        if (empty($directory)) {
82
            $directory = getcwd();
83
84
            if ($directory === false) {
85
                throw new BoilerException('Current directory cannot be added.');
86
            }
87
        }
88
89
        return $directory;
90
    }
91
}
92