Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

Install::execute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 24
ccs 7
cts 7
cp 1
rs 9.7666
cc 3
nc 2
nop 2
crap 3
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[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
namespace CaptainHook\App\Console\Command;
13
14
use CaptainHook\App\Console\IOUtil;
15
use CaptainHook\App\Console\Runtime\Resolver;
16
use CaptainHook\App\Hook\Template;
17
use CaptainHook\App\Runner\Installer;
18
use RuntimeException;
19
use SebastianFeldmann\Camino\Check;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
/**
26
 * Class Install
27
 *
28
 * @package CaptainHook
29
 * @author  Sebastian Feldmann <[email protected]>
30
 * @link    https://github.com/captainhookphp/captainhook
31
 * @since   Class available since Release 0.9.0
32
 */
33
class Install extends RepositoryAware
34
{
35
    /**
36
     * Configure the command
37
     *
38 7
     * @return void
39
     */
40 7
    protected function configure(): void
41 7
    {
42 7
        parent::configure();
43 7
        $this->setName('install')
44 7
             ->setDescription('Install git hooks')
45 7
             ->setHelp('This command will install the git hooks to your .git directory')
46 7
             ->addArgument(
47 7
                 'hook',
48 7
                 InputArgument::OPTIONAL,
49 7
                 'Limit the hook you want to install. By default all hooks get installed.'
50
             )
51 7
             ->addOption(
52 7
                 'force',
53 7
                 'f',
54 7
                 InputOption::VALUE_NONE,
55 7
                 'Force install without confirmation'
56
             )
57 7
             ->addOption(
58 7
                 'skip-existing',
59 7
                 's',
60 7
                 InputOption::VALUE_NONE,
61 7
                 'Do not overwrite existing hooks'
62
             )
63 7
             ->addOption(
64 7
                 'move-existing-to',
65 7
                 null,
66 7
                 InputOption::VALUE_OPTIONAL,
67 7
                 'Move existing hooks to given directory'
68
             )
69 7
             ->addOption(
70 7
                 'bootstrap',
71 7
                 'b',
72 7
                 InputOption::VALUE_OPTIONAL,
73 7
                 'Path to composers vendor/autoload.php'
74
             )
75 7
             ->addOption(
76 7
                 'run-mode',
77 7
                 'm',
78 7
                 InputOption::VALUE_OPTIONAL,
79 7
                 'Git hook run mode [php|shell|docker]'
80
             )
81 7
             ->addOption(
82
                 'run-exec',
83
                 'e',
84
                 InputOption::VALUE_OPTIONAL,
85
                 'The Docker command to start your container e.g. \'docker exec CONTAINER\''
86
             )
87
             ->addOption(
88
                 'run-path',
89
                 'p',
90
                 InputOption::VALUE_OPTIONAL,
91 6
                 'The path to the CaptainHook executable \'/usr/bin/captainhook\''
92
             );
93 6
    }
94
95
    /**
96 6
     * Execute the command
97
     *
98 6
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
99 5
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
100
     * @return int
101 4
     * @throws \Exception
102 1
     */
103 1
    protected function execute(InputInterface $input, OutputInterface $output)
104
    {
105
        $io     = $this->getIO($input, $output);
106
        $config = $this->createConfig($input, true, ['git-directory', 'run-mode', 'run-exec', 'run-path', 'bootstrap']);
107 3
        $repo   = $this->createRepository(dirname($config->getGitDirectory()));
108 3
109 3
        // use the configured verbosity to manage general output verbosity
110 3
        $output->setVerbosity(IOUtil::mapConfigVerbosity($config->getVerbosity()));
111 3
112
        if ($config->getRunMode() === Template::DOCKER && empty($config->getRunExec())) {
113 3
            throw new RuntimeException(
114
                'Option "run-exec" missing for run-mode docker.'
115
            );
116
        }
117
118
        $template  = Template\Builder::build($config, $repo, $this->resolver);
119
        $installer = new Installer($io, $config, $repo, $template);
120
        $installer->setForce(IOUtil::argToBool($input->getOption('force')))
121
                  ->setSkipExisting(IOUtil::argToBool($input->getOption('skip-existing')))
122
                  ->setMoveExistingTo(IOUtil::argToString($input->getOption('move-existing-to')))
123
                  ->setHook(IOUtil::argToString($input->getArgument('hook')))
124
                  ->run();
125
126
        return 0;
127
    }
128
}
129