Install::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 60
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 51
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 61
ccs 60
cts 60
cp 1
crap 1
rs 9.069

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Config;
15
use CaptainHook\App\Console\IOUtil;
16
use CaptainHook\App\Hook\Template;
17
use CaptainHook\App\Runner\Installer;
18
use Exception;
19
use RuntimeException;
20
use SebastianFeldmann\Git\Repository;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
/**
27
 * Class Install
28
 *
29
 * @package CaptainHook
30
 * @author  Sebastian Feldmann <[email protected]>
31
 * @link    https://github.com/captainhook-git/captainhook
32
 * @since   Class available since Release 0.9.0
33
 */
34
class Install extends RepositoryAware
35
{
36
    /**
37
     * Configure the command
38
     *
39
     * @return void
40
     */
41 16
    protected function configure(): void
42
    {
43 16
        parent::configure();
44 16
        $this->setName('install')
45 16
             ->setDescription('Install hooks to your .git/hooks directory')
46 16
             ->setHelp('Install git hooks to your .git/hooks directory')
47 16
             ->addArgument(
48 16
                 'hook',
49 16
                 InputArgument::OPTIONAL,
50 16
                 'Limit the hooks you want to install. ' .
51 16
                 'You can specify multiple hooks with comma as delimiter. ' .
52 16
                 'By default all hooks get installed'
53 16
             )
54 16
             ->addOption(
55 16
                 'only-enabled',
56 16
                 null,
57 16
                 InputOption::VALUE_NONE,
58 16
                 'Limit the hooks you want to install to those enabled in your conf. ' .
59 16
                 'By default all hooks get installed'
60 16
             )
61 16
             ->addOption(
62 16
                 'force',
63 16
                 'f',
64 16
                 InputOption::VALUE_NONE,
65 16
                 'Force install without confirmation'
66 16
             )
67 16
             ->addOption(
68 16
                 'skip-existing',
69 16
                 's',
70 16
                 InputOption::VALUE_NONE,
71 16
                 'Do not overwrite existing hooks'
72 16
             )
73 16
             ->addOption(
74 16
                 'move-existing-to',
75 16
                 null,
76 16
                 InputOption::VALUE_OPTIONAL,
77 16
                 'Move existing hooks to given directory'
78 16
             )
79 16
             ->addOption(
80 16
                 'bootstrap',
81 16
                 'b',
82 16
                 InputOption::VALUE_OPTIONAL,
83 16
                 'Path to composers vendor/autoload.php'
84 16
             )
85 16
             ->addOption(
86 16
                 'run-mode',
87 16
                 'm',
88 16
                 InputOption::VALUE_OPTIONAL,
89 16
                 'Git hook run mode [php|shell|docker]'
90 16
             )
91 16
             ->addOption(
92 16
                 'run-exec',
93 16
                 'e',
94 16
                 InputOption::VALUE_OPTIONAL,
95 16
                 'The Docker command to start your container e.g. \'docker exec CONTAINER\''
96 16
             )
97 16
             ->addOption(
98 16
                 'run-path',
99 16
                 'p',
100 16
                 InputOption::VALUE_OPTIONAL,
101 16
                 'The path to the CaptainHook executable \'/usr/bin/captainhook\''
102 16
             );
103
    }
104
105
    /**
106
     * Execute the command
107
     *
108
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
109
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
110
     * @return int
111
     * @throws \Exception
112
     */
113 14
    protected function execute(InputInterface $input, OutputInterface $output): int
114
    {
115
        try {
116 14
            $args     = ['git-directory', 'run-mode', 'run-exec', 'run-path', 'bootstrap'];
117 14
            $io       = $this->getIO($input, $output);
118 14
            $config   = $this->createConfig($input, true, $args);
119 13
            $repo     = $this->createRepository(dirname($config->getGitDirectory()));
120 12
            $template = $this->createTemplate($config, $repo);
121
122 11
            $this->determineVerbosity($output, $config);
123
124 11
            $installer = new Installer($io, $config, $repo, $template);
125 11
            $installer->setHook(IOUtil::argToString($input->getArgument('hook')))
126 11
                      ->setForce(IOUtil::argToBool($input->getOption('force')))
127 11
                      ->setSkipExisting(IOUtil::argToBool($input->getOption('skip-existing')))
128 11
                      ->setMoveExistingTo(IOUtil::argToString($input->getOption('move-existing-to')))
129 11
                      ->setOnlyEnabled(IOUtil::argToBool($input->getOption('only-enabled')))
130 11
                      ->run();
131
132 8
            return 0;
133 6
        } catch (Exception $e) {
134 6
            return $this->crash($output, $e);
135
        }
136
    }
137
138
    /**
139
     * Create the template to generate the hook source code
140
     *
141
     * @param  \CaptainHook\App\Config           $config
142
     * @param  \SebastianFeldmann\Git\Repository $repo
143
     * @return \CaptainHook\App\Hook\Template
144
     */
145 12
    private function createTemplate(Config $config, Repository $repo): Template
146
    {
147
        if (
148 12
            $config->getRunConfig()->getMode() === Template::DOCKER
149 12
            && empty($config->getRunConfig()->getDockerCommand())
150
        ) {
151 1
            throw new RuntimeException('Run "exec" option missing for run-mode docker.');
152
        }
153
154 11
        return Template\Builder::build($config, $repo, $this->resolver);
155
    }
156
}
157