Passed
Push — master ( eba56b...0dd639 )
by Sebastian
01:52
created

Install::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
cc 2
nc 2
nop 2
crap 2
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
                 'bootstrap',
65 7
                 'b',
66 7
                 InputOption::VALUE_OPTIONAL,
67 7
                 'Path to composers vendor/autoload.php'
68
             )
69 7
             ->addOption(
70 7
                 'run-mode',
71 7
                 'm',
72 7
                 InputOption::VALUE_OPTIONAL,
73 7
                 'Git hook run mode [php|shell|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
             ->addOption(
82
                 'run-path',
83
                 'p',
84
                 InputOption::VALUE_OPTIONAL,
85
                 'The path to the CaptainHook executable \'/usr/bin/captainhook\''
86
             );
87
    }
88
89
    /**
90
     * Execute the command
91 6
     *
92
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
93 6
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
94
     * @return int
95
     * @throws \CaptainHook\App\Exception\InvalidHookName
96 6
     * @throws \Exception
97
     */
98 6
    protected function execute(InputInterface $input, OutputInterface $output)
99 5
    {
100
        $io     = $this->getIO($input, $output);
101 4
        $config = $this->createConfig($input, true, ['git-directory', 'run-mode', 'run-exec', 'run-path', 'bootstrap']);
102 1
        $repo   = $this->createRepository(dirname($config->getGitDirectory()));
103 1
104
        if ($config->getRunMode() === Template::DOCKER && empty($config->getRunExec())) {
105
            throw new RuntimeException(
106
                'Option "run-exec" missing for run-mode docker.'
107 3
            );
108 3
        }
109 3
110 3
        $template  = Template\Builder::build($config, $repo, $this->executablePath(), $this->resolver->isPharRelease());
111 3
        $installer = new Installer($io, $config, $repo, $template);
112
        $installer->setForce(IOUtil::argToBool($input->getOption('force')))
113 3
                  ->setSkipExisting(IOUtil::argToBool($input->getOption('skip-existing')))
114
                  ->setHook(IOUtil::argToString($input->getArgument('hook')))
115
                  ->run();
116
117
        return 0;
118
    }
119
120
    /**
121
     * Return the absolute path to the currently executed 'binary'
122
     *
123
     * @return string
124
     */
125
    private function executablePath(): string
126
    {
127
        $executable = $this->resolver->getExecutable();
128
        return Check::isAbsolutePath($executable) ? $executable : getcwd() . '/' . $executable;
129
    }
130
}
131