Completed
Push — master ( 7b4301...3d29af )
by Sebastian
05:32
created

Install::getOpt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[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
namespace CaptainHook\App\Console\Command;
11
12
use CaptainHook\App\CH;
13
use CaptainHook\App\Console\IOUtil;
14
use CaptainHook\App\Hook\Template;
15
use CaptainHook\App\Runner\Installer;
16
use RuntimeException;
17
use SebastianFeldmann\Git\Repository;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * Class Install
25
 *
26
 * @package CaptainHook
27
 * @author  Sebastian Feldmann <[email protected]>
28
 * @link    https://github.com/captainhookphp/captainhook
29
 * @since   Class available since Release 0.9.0
30
 */
31
class Install extends Base
32
{
33
    /**
34
     * Configure the command
35
     *
36
     * @return void
37
     */
38 7
    protected function configure() : void
39
    {
40 7
        $this->setName('install')
41 7
             ->setDescription('Install git hooks')
42 7
             ->setHelp('This command will install the git hooks to your .git directory')
43 7
             ->addArgument('hook', InputArgument::OPTIONAL, 'Hook you want to install')
44 7
             ->addOption(
45 7
                 'configuration',
46 7
                 'c',
47 7
                 InputOption::VALUE_OPTIONAL,
48 7
                 'Path to your json configuration',
49 7
                 getcwd() . DIRECTORY_SEPARATOR . CH::CONFIG
50
             )
51 7
             ->addOption(
52 7
                 'force',
53 7
                 'f',
54 7
                 InputOption::VALUE_NONE,
55 7
                 'Force to overwrite existing hooks'
56
             )
57 7
             ->addOption(
58 7
                 'git-directory',
59 7
                 'g',
60 7
                 InputOption::VALUE_OPTIONAL,
61 7
                 'Path to your .git directory'
62
             )
63 7
             ->addOption(
64 7
                 'vendor-directory',
65 7
                 null,
66 7
                 InputOption::VALUE_OPTIONAL,
67 7
                 'Path to composers vendor directory'
68
             )
69 7
             ->addOption(
70 7
                 'run-mode',
71 7
                 'm',
72 7
                 InputOption::VALUE_OPTIONAL,
73 7
                 'Git hook run mode [local|docker]'
74
             )
75 7
             ->addOption(
76 7
                 'run-exec',
77 7
                 'e',
78 7
                 InputOption::VALUE_OPTIONAL,
79 7
                 'The Docker command to start your container e.g. \'docker exec CONTAINER\''
80
             );
81 7
    }
82
83
    /**
84
     * Execute the command
85
     *
86
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
87
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
88
     * @return int|null
89
     * @throws \CaptainHook\App\Exception\InvalidHookName
90
     */
91 6
    protected function execute(InputInterface $input, OutputInterface $output)
92
    {
93 6
        $io = $this->getIO($input, $output);
94
95
        // collect settings to overwrite configuration values
96 6
        $settings = $this->fetchInputSettings($input, ['git-directory', 'run-mode', 'run-exec', 'vendor-directory']);
97
98 6
        $config = $this->getConfig(IOUtil::argToString($input->getOption('configuration')), true, $settings);
99 5
        $repo   = new Repository(dirname($config->getGitDirectory()));
100
101 4
        if ($config->getRunMode() === Template::DOCKER && empty($config->getRunExec())) {
102 1
            throw new RuntimeException(
103 1
                'Option "run-exec" missing for run-mode docker.'
104
            );
105
        }
106
107 3
        $installer = new Installer($io, $config, $repo);
108 3
        $installer->setForce(IOUtil::argToBool($input->getOption('force')))
109 3
                  ->setHook(IOUtil::argToString($input->getArgument('hook')))
110 3
                  ->setTemplate(Template\Builder::build($config, $repo))
111 3
                  ->run();
112
113 3
        return 0;
114
    }
115
}
116