Passed
Push — main ( 36f773...e1b064 )
by Sebastian
03:34
created

Installer::checkForBrokenSymlink()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
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
declare(strict_types=1);
13
14
namespace CaptainHook\App\Runner;
15
16
use CaptainHook\App\Config;
17
use CaptainHook\App\Console\IO;
18
use CaptainHook\App\Console\IOUtil;
19
use CaptainHook\App\Exception;
20
use CaptainHook\App\Hook\Template;
21
use CaptainHook\App\Hook\Util as HookUtil;
22
use CaptainHook\App\Hooks;
23
use CaptainHook\App\Storage\File;
24
use RuntimeException;
25
use SebastianFeldmann\Camino\Check;
26
use SebastianFeldmann\Git\Repository;
27
28
/**
29
 * Class Installer
30
 *
31
 * @package CaptainHook
32
 * @author  Sebastian Feldmann <[email protected]>
33
 * @link    https://github.com/captainhook-git/captainhook
34
 * @since   Class available since Release 0.9.0
35
 */
36
class Installer extends Files
37
{
38
    /**
39
     * Don't overwrite existing hooks
40
     *
41
     * @var bool
42
     */
43
    private bool $skipExisting = false;
44
45
    /**
46
     * Install only enabled hooks
47
     *
48
     * @var bool
49
     */
50
    private bool $onlyEnabled = false;
51
52
    /**
53
     * Hook template
54
     *
55
     * @var \CaptainHook\App\Hook\Template
56
     */
57
    private Template $template;
58
59
    /**
60
     * HookHandler constructor.
61
     *
62
     * @param \CaptainHook\App\Console\IO       $io
63
     * @param \CaptainHook\App\Config           $config
64
     * @param \SebastianFeldmann\Git\Repository $repository
65
     * @param \CaptainHook\App\Hook\Template    $template
66
     */
67 28
    public function __construct(IO $io, Config $config, Repository $repository, Template $template)
68
    {
69 28
        $this->template = $template;
70 28
        parent::__construct($io, $config, $repository);
71
    }
72
73
    /**
74
     * @param  bool $skip
75
     * @return \CaptainHook\App\Runner\Installer
76
     */
77 12
    public function setSkipExisting(bool $skip): Installer
78
    {
79 12
        if ($skip && !empty($this->moveExistingTo)) {
80 1
            throw new RuntimeException('choose --move-existing-to or --skip-existing');
81
        }
82 11
        $this->skipExisting = $skip;
83 11
        return $this;
84
    }
85
86
    /**
87
     * Set the path where the current hooks should be moved to
88
     *
89
     * @param  string $backup
90
     * @return static
91
     */
92 15
    public function setMoveExistingTo(string $backup): static
93
    {
94 15
        if (!empty($backup) && $this->skipExisting) {
95 1
            throw new RuntimeException('choose --skip-existing or --move-existing-to');
96
        }
97 14
        return parent::setMoveExistingTo($backup);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setMoveExistingTo($backup) returns the type CaptainHook\App\Runner\Files which includes types incompatible with the type-hinted return CaptainHook\App\Runner\Installer.
Loading history...
98
    }
99
100
    /**
101
     * @param bool $onlyEnabled
102
     * @return \CaptainHook\App\Runner\Installer
103
     */
104 11
    public function setOnlyEnabled(bool $onlyEnabled): Installer
105
    {
106 11
        if ($onlyEnabled && !empty($this->hooksToHandle)) {
107 2
            throw new RuntimeException('choose --only-enabled or specific hooks');
108
        }
109
110 9
        $this->onlyEnabled = $onlyEnabled;
111 9
        return $this;
112
    }
113
114
    /**
115
     * Hook setter
116
     *
117
     * @param  string $hook
118
     * @return \CaptainHook\App\Runner\Installer
119
     * @throws \CaptainHook\App\Exception\InvalidHookName
120
     */
121 26
    public function setHook(string $hook): Installer
122
    {
123 26
        if (empty($hook)) {
124 6
            return $this;
125
        }
126
127 20
        if ($this->onlyEnabled) {
128 1
            throw new RuntimeException('choose --only-enabled or specific hooks');
129
        }
130
131 19
        return parent::setHook($hook);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setHook($hook) returns the type CaptainHook\App\Runner\Files which includes types incompatible with the type-hinted return CaptainHook\App\Runner\Installer.
Loading history...
132
    }
133
134
    /**
135
     * Execute installation
136
     *
137
     * @return void
138
     */
139 17
    public function run(): void
140
    {
141 17
        $hooks = $this->getHooksToInstall();
142
143 17
        foreach ($hooks as $hook => $ask) {
144 17
            $this->installHook($hook, ($ask && !$this->force));
145
        }
146
    }
147
148
    /**
149
     * Return list of hooks to install
150
     *
151
     * [
152
     *   string    => bool
153
     *   HOOK_NAME => ASK_USER_TO_CONFIRM_INSTALL
154
     * ]
155
     *
156
     * @return array<string, bool>
157
     */
158 17
    public function getHooksToInstall(): array
159
    {
160 17
        $hooks = $this->getHooksToHandle();
161
        // if only enabled hooks should be installed, remove disabled ones from the $hooks array
162 17
        if ($this->onlyEnabled) {
163 5
            $hooks = array_filter(
164 5
                $hooks,
165 5
                fn(string $key): bool => $this->config->isHookEnabled($key),
166 5
                ARRAY_FILTER_USE_KEY
167 5
            );
168
        }
169
        // make sure to ask for every remaining hook if it should be installed
170 17
        return $hooks;
171
    }
172
173
    /**
174
     * Install given hook
175
     *
176
     * @param string $hook
177
     * @param bool   $ask
178
     */
179 17
    private function installHook(string $hook, bool $ask): void
180
    {
181 17
        if ($this->shouldHookBeSkipped($hook)) {
182 1
            $hint = $this->io->isDebug() ? ', remove the --skip-existing option to overwrite.' : '';
183 1
            $this->io->write(
184 1
                IOUtil::PREFIX_FAIL . ' <comment>' . $hook . '</comment> exists' . $hint,
185 1
                true,
186 1
                IO::VERBOSE
187 1
            );
188 1
            return;
189
        }
190
191 16
        $doIt = true;
192 16
        if ($ask) {
193 1
            $answer = $this->io->ask('Install <comment>' . $hook . '</comment> hook? <comment>[Y,n]</comment> ', 'y');
194 1
            $doIt   = IOUtil::answerToBool($answer);
195
        }
196
197 16
        if ($doIt) {
198 15
            if ($this->shouldHookBeMoved()) {
199 4
                $this->backupHook($hook);
200
            }
201 14
            $this->writeHookFile($hook);
202
        }
203
    }
204
205
    /**
206
     * Check if the hook is installed and should be skipped
207
     *
208
     * @param  string $hook
209
     * @return bool
210
     */
211 17
    private function shouldHookBeSkipped(string $hook): bool
212
    {
213 17
        return $this->skipExisting && $this->repository->hookExists($hook);
214
    }
215
216
    /**
217
     * Write given hook to .git/hooks directory
218
     *
219
     * @param  string $hook
220
     * @return void
221
     */
222 14
    private function writeHookFile(string $hook): void
223
    {
224 14
        $hooksDir = $this->repository->getHooksDir();
225 14
        $hookFile = $hooksDir . DIRECTORY_SEPARATOR . $hook;
226 14
        $doIt     = true;
227
228
        // if a hook is configured and no force option is set,
229
        // ask the user if overwriting the hook is ok
230 14
        if ($this->needConfirmation($hook)) {
231 1
            $ans  = $this->io->ask(
232 1
                'The <comment>' . $hook . '</comment> hook exists! Overwrite? <comment>[y,N]</comment> ',
233 1
                'n'
234 1
            );
235 1
            $doIt = IOUtil::answerToBool($ans);
236
        }
237
238 14
        if ($doIt) {
239 13
            $code = $this->getHookSourceCode($hook);
240 13
            $file = new File($hookFile);
241 13
            $this->checkForBrokenSymlink($file);
242 13
            $file->write($code);
243 13
            chmod($hookFile, 0755);
244 13
            $this->io->write(IOUtil::PREFIX_OK . ' <comment>' . $hook . '</comment> installed');
245 13
            return;
246
        }
247 1
        $this->io->write(IOUtil::PREFIX_FAIL . ' <comment>' . $hook . '</comment> skipped');
248
    }
249
250
    /**
251
     * Return the source code for a given hook script
252
     *
253
     * @param  string $hook
254
     * @return string
255
     */
256 13
    private function getHookSourceCode(string $hook): string
257
    {
258 13
        return $this->template->getCode($hook);
259
    }
260
261
    /**
262
     * Checks if the provided file is a broken symbolic link
263
     *
264
     * @param  File $file The File object representing the file.
265
     * @return void
266
     * @throws RuntimeException If the file is determined to be a broken symbolic link.
267
     */
268 15
    protected function checkForBrokenSymlink(File $file): void
269
    {
270 15
        if ($file->isLink()) {
271 2
            if (!is_dir(dirname($file->linkTarget()))) {
272 1
                throw new RuntimeException(
273 1
                    'The hook at \'' . $file->getPath() . '\' is a broken symbolic link. ' . PHP_EOL .
274 1
                    'Please remove the symbolic link and try again.'
275 1
                );
276
            }
277
        }
278
    }
279
}
280