Passed
Push — main ( 9185d0...afd76b )
by Sebastian
03:36
created

Uninstaller::setOnlyDisabled()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
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\Console\IOUtil;
17
use RuntimeException;
18
19
/**
20
 * Class Uninstaller
21
 *
22
 * @package CaptainHook
23
 * @author  Sebastian Feldmann <[email protected]>
24
 * @link    https://github.com/captainhookphp/captainhook
25
 * @since   Class available since Release 5.17.0
26
 */
27
class Uninstaller extends Files
28
{
29
    /**
30
     * Remove only disabled hooks
31
     *
32
     * @var bool
33
     */
34
    private bool $onlyDisabled = false;
35
36
    /**
37
     * Execute installation
38
     *
39
     * @return void
40
     */
41 8
    public function run(): void
42
    {
43 8
        foreach ($this->getHooksToUninstall() as $hook => $ask) {
44 8
            $this->uninstallHook($hook, ($ask && !$this->force));
45
        }
46
    }
47
48
    /**
49
     * Disabled only setter
50
     *
51
     * @param  bool $disabledOnly
52
     * @return \CaptainHook\App\Runner\Uninstaller
53
     */
54 3
    public function setOnlyDisabled(bool $disabledOnly): Uninstaller
55
    {
56 3
        if ($disabledOnly && !empty($this->hooksToHandle)) {
57 1
            throw new RuntimeException('choose --only-disabled or specific hooks');
58
        }
59 2
        $this->onlyDisabled = $disabledOnly;
60 2
        return $this;
61
    }
62
63
    /**
64
     * Returns the list of hooks to uninstall
65
     *
66
     * [
67
     *   string    => bool
68
     *   HOOK_NAME => ASK_USER_TO_CONFIRM_INSTALL
69
     * ]
70
     *
71
     * @return array<string, bool>
72
     */
73 8
    private function getHooksToUninstall(): array
74
    {
75 8
        $hooks = $this->getHooksToHandle();
76
77
        // if only disabled hooks should be removed, remove enabled ones from $hooks array
78 8
        if ($this->onlyDisabled) {
79 1
            $hooks = array_filter(
80 1
                $hooks,
81 1
                fn(string $key): bool => !$this->config->isHookEnabled($key),
82 1
                ARRAY_FILTER_USE_KEY
83 1
            );
84
        }
85 8
        return $hooks;
86
    }
87
88
    /**
89
     * Install given hook
90
     *
91
     * @param string $hook
92
     * @param bool   $ask
93
     */
94 8
    private function uninstallHook(string $hook, bool $ask): void
95
    {
96 8
        if (!$this->repository->hookExists($hook)) {
97 3
            $this->io->write('<comment>' . $hook . '</comment> not installed');
98 3
            return;
99
        }
100
101 8
        $doIt = true;
102 8
        if ($ask) {
103 1
            $answer = $this->io->ask('Remove <info>' . $hook . '</info> hook? <comment>[y,n]</comment> ', 'y');
104 1
            $doIt   = IOUtil::answerToBool($answer);
105
        }
106
107 8
        if ($doIt) {
108 7
            if ($this->shouldHookBeMoved()) {
109 3
                $this->backupHook($hook);
110 2
                return;
111
            }
112 4
            unlink($this->repository->getHooksDir() . DIRECTORY_SEPARATOR . $hook);
113 4
            $this->io->write(IOUtil::PREFIX_OK . ' <info>' . $hook . '</info> removed');
114 4
            return;
115
        }
116 1
        $this->io->write(IOUtil::PREFIX_FAIL . ' <comment>' . $hook . '</comment> skipped');
117
    }
118
}
119