GitPolicyInit::execute()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.627
c 0
b 0
f 0
cc 5
nc 7
nop 2

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
 * initializes gitpolicy in a repo by adding the git hook as well as the default .gitpolicy.yml
5
 */
6
7
namespace GitPolicy\Command;
8
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
13
14
class GitPolicyInit extends Command
15
{
16
    /**
17
     * configure the command.
18
     */
19
    protected function configure()
20
    {
21
        // define the command
22
        $this
23
            ->setName('init')
24
            ->setDescription('Initializes GitPolicy for a git repository.')
25
            ->setHelp(<<<EOT
26
Creates the following
27
28
 * git hook to capture the pushes and
29
 * default .gitconfig.yml
30
31
See https://github.com/Spekulatius/GitPolicy for more information.
32
EOT
33
);
34
    }
35
36
    /**
37
     * runs all related init steps for this repo.
38
     *
39
     * @param InputInterface  $input
40
     * @param OutputInterface $output
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        // prep some colors for the output
45
        $output->getFormatter()->setStyle('error', new OutputFormatterStyle('white', 'red', array('bold')));
46
        $output->getFormatter()->setStyle('warning', new OutputFormatterStyle('black', 'yellow', array('bold')));
47
        $output->getFormatter()->setStyle('good', new OutputFormatterStyle('white', 'green', array('bold')));
48
49
        // start print out
50
        $output->writeln("\n<good>GitPolicy initialization</good>");
51
52
        // check if this is a git repo
53
        exec('git status 2>&1', $return, $returnCode);
54
        if ($returnCode > 0) {
55
            $output->writeln("<error>This doesn't apprear to be a git repo.</error>");
56
            $output->writeln("\n<good>Stopping execution :(</good>\n");
57
            exit(0);
58
        }
59
60
        // check if git hook is already installed.
61
        $output->writeln("\nGit hook");
62
        if (file_exists('.git/hooks/pre-push')) {
63
            // quick and dirty check if this is already the GitPolicy hook
64
            if (md5(file_get_contents('templates/pre-push')) == md5(file_get_contents('.git/hooks/pre-push'))) {
65
                $output->writeln('<good>Is already in place :)</good>');
66
            } else {
67
                $output->writeln(
68
                    '<warning>This repository already has a "pre-push" git hook.'.
69
                    'Please ensure manually it works with GitPolicy. Continuing...</warning>'
70
                );
71
            }
72
        } else {
73
            $output->writeln(
74
                '<good>Adding the git hook :)</good>'
75
            );
76
77
            // copy over the file and attempt to adjust the permissions
78
            copy(realpath('templates/pre-push'), '.git/hooks/pre-push');
79
            chmod('.git/hooks/pre-push', 0755);
80
        }
81
82
        // check if there is already a configuration file
83
        $output->writeln("\n.gitpolicy.yml file");
84
        if (file_exists('.gitpolicy.yml')) {
85
            $output->writeln(
86
                '<warning>.gitpolicy.yml already exists. Won\'t copy default config in. Continuing...</warning>'
87
            );
88
        } else {
89
            copy(realpath('templates/.gitpolicy.yml'), '.gitpolicy.yml');
90
91
            $output->writeln(
92
                '<good>We copied an example of the .gitpolicy.yml into your folder. '.
93
                'Have a look and adjust it to your needs :)</good>'
94
            );
95
        }
96
97
        $output->writeln("\n<good>Done</good>\n");
98
    }
99
}
100