Passed
Push — master ( b34030...56c38f )
by Sebastian
03:50
created

Installer::setForce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 sebastianfeldmann\CaptainHook\Runner;
11
12
use sebastianfeldmann\CaptainHook\Console\IOUtil;
13
use sebastianfeldmann\CaptainHook\Hook\Template;
14
use sebastianfeldmann\CaptainHook\Storage\File;
15
use sebastianfeldmann\CaptainHook\Hook\Util;
16
17
/**
18
 * Class Installer
19
 *
20
 * @package CaptainHook
21
 * @author  Sebastian Feldmann <[email protected]>
22
 * @link    https://github.com/sebastianfeldmann/captainhook
23
 * @since   Class available since Release 0.9.0
24
 */
25
class Installer extends HookHandler
26
{
27
    /**
28
     * Overwrite hook
29
     *
30
     * @var bool
31
     */
32
    private $force;
33
34
    /**
35
     * @param  bool $force
36
     * @return \sebastianfeldmann\CaptainHook\Runner\Installer
37
     */
38 1
    public function setForce($force)
39
    {
40 1
        $this->force = $force;
41 1
        return $this;
42
    }
43
44
    /**
45
     * Execute installation.
46
     */
47 1
    public function run()
48
    {
49 1
        $hooks = $this->getHooksToInstall();
50
51 1
        foreach ($hooks as $hook => $ask) {
52 1
            $this->installHook($hook, $ask);
53
        }
54 1
    }
55
56
    /**
57
     * Return list of hooks to install.
58
     *
59
     * @return array
60
     */
61 1
    public function getHooksToInstall()
62
    {
63 1
        return null === $this->hookToHandle ? Util::getValidHooks() : [$this->hookToHandle => false];
64
    }
65
66
    /**
67
     * Install given hook.
68
     *
69
     * @param string $hook
70
     * @param bool   $ask
71
     */
72 2
    public function installHook($hook, $ask)
73
    {
74 2
        $doIt = true;
75 2
        if ($ask) {
76 1
            $answer = $this->io->ask('  <info>Install \'' . $hook . '\' hook?</info> <comment>[y,n]</comment> ', 'y');
77 1
            $doIt   = IOUtil::answerToBool($answer);
78
        }
79
80 2
        if ($doIt) {
81 1
            $this->writeHookFile($hook);
82
        }
83 2
    }
84
85
    /**
86
     * Write given hook to .git/hooks directory
87
     *
88
     * @param string $hook
89
     */
90 2
    public function writeHookFile($hook)
91
    {
92 2
        $hooksDir = $this->repository->getHooksDir();
93 2
        $hookFile = $hooksDir . DIRECTORY_SEPARATOR . $hook;
94 2
        $doIt     = true;
95
96
        // if hook is configured and no force option is set
97
        // ask the user if overwriting the hook is ok
98 2
        if ($this->needInstallConfirmation($hook)) {
99 1
            $ans  = $this->io->ask('  <comment>The \'' . $hook . '\' hook exists! Overwrite? [y,n]</comment> ', 'n');
100 1
            $doIt = IOUtil::answerToBool($ans);
101
        }
102
103 2
        if ($doIt) {
104 1
            $code = Template::getCode($hook);
105 1
            $file = new File($hookFile);
106 1
            $file->write($code);
107 1
            chmod($hookFile, 0755);
108 1
            $this->io->write('  <info>\'' . $hook . '\' hook installed successfully</info>');
109
        }
110 2
    }
111
112
    /**
113
     * If the hook already exists the user has to confirm the installation.
114
     *
115
     * @param  string $hook The name of the hook to check
116
     * @return bool
117
     */
118 2
    protected function needInstallConfirmation($hook)
119
    {
120 2
        return $this->repository->hookExists($hook) && !$this->force;
121
    }
122
}
123