Passed
Push — main ( f0911f...cd99fe )
by Sebastian
05:24
created

Hooks   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 59
c 2
b 0
f 0
dl 0
loc 158
ccs 46
cts 46
cp 1
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getNativeHooksForVirtualHook() 0 7 1
A getVirtualHook() 0 6 2
A triggersVirtualHook() 0 3 1
A getValidHooks() 0 3 1
A getOriginalHookArguments() 0 14 1
A virtualHooks() 0 4 1
A nativeHooks() 0 11 1
A receivesStdIn() 0 3 1
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;
15
16
use RuntimeException;
17
18
/**
19
 * Class Hooks
20
 *
21
 * Defines the list of hooks that can be handled with captainhook and provides some name constants.
22
 *
23
 * @package CaptainHook
24
 * @author  Andrea Heigl <[email protected]>
25
 * @author  Sebastian Feldmann <[email protected]>
26
 * @link    https://github.com/captainhookphp/captainhook
27
 * @since   Class available since Release 3.0.1
28
 */
29
final class Hooks
30
{
31
    public const PRE_COMMIT         = 'pre-commit';
32
    public const PRE_PUSH           = 'pre-push';
33
    public const COMMIT_MSG         = 'commit-msg';
34
    public const PREPARE_COMMIT_MSG = 'prepare-commit-msg';
35
    public const POST_COMMIT        = 'post-commit';
36
    public const POST_MERGE         = 'post-merge';
37
    public const POST_CHECKOUT      = 'post-checkout';
38
    public const POST_REWRITE       = 'post-rewrite';
39
    public const POST_CHANGE        = 'post-change';
40
41
    public const ARG_COMMAND       = 'command';
42
    public const ARG_GIT_COMMAND   = 'git-command';
43
    public const ARG_HASH          = 'hash';
44
    public const ARG_MESSAGE_FILE  = 'message-file';
45
    public const ARG_MODE          = 'mode';
46
    public const ARG_NEW_HEAD      = 'new-head';
47
    public const ARG_PREVIOUS_HEAD = 'previous-head';
48
    public const ARG_SQUASH        = 'squash';
49
    public const ARG_TARGET        = 'target';
50
    public const ARG_URL           = 'url';
51
52
    /**
53
     * This defines which native hook trigger which virtual hook
54
     *
55
     * @var string[]
56
     */
57
    private static array $virtualHookTriggers = [
58
        self::POST_CHECKOUT => self::POST_CHANGE,
59
        self::POST_MERGE    => self::POST_CHANGE,
60
        self::POST_REWRITE  => self::POST_CHANGE,
61
    ];
62
63
    /**
64
     * Is it necessary to give the Captain access to user input
65
     *
66
     * @var array<string, bool>
67
     */
68
    private static array $hooksReceivingStdInput = [
69
        self::PRE_PUSH     => true,
70
        self::POST_REWRITE => true,
71
    ];
72
73
    /**
74
     * Returns the list of valid hooks
75
     *
76
     * @return array<string, string>
77
     */
78 198
    public static function getValidHooks(): array
79
    {
80 198
        return array_merge(self::nativeHooks(), self::virtualHooks());
81
    }
82
83
    /**
84
     * Returns a list of all natively supported git hooks
85
     *
86
     * @return array<string, string>
87
     */
88 219
    public static function nativeHooks(): array
89
    {
90 219
        return [
91 219
            self::COMMIT_MSG         => 'CommitMsg',
92 219
            self::PRE_PUSH           => 'PrePush',
93 219
            self::PRE_COMMIT         => 'PreCommit',
94 219
            self::PREPARE_COMMIT_MSG => 'PrepareCommitMsg',
95 219
            self::POST_COMMIT        => 'PostCommit',
96 219
            self::POST_MERGE         => 'PostMerge',
97 219
            self::POST_CHECKOUT      => 'PostCheckout',
98 219
            self::POST_REWRITE       => 'PostRewrite'
99 219
        ];
100
    }
101
102
    /**
103
     * Return a list of all artificial CaptainHook hooks (virtual hooks)
104
     *
105
     * @return array<string, string>
106
     */
107 198
    public static function virtualHooks(): array
108
    {
109 198
        return [
110 198
            self::POST_CHANGE => 'PostChange'
111 198
        ];
112
    }
113
114
    /**
115
     * Returns a list of all native hooks triggered by a given virtual hook
116
     *
117
     * @return array<string>
118
     */
119 30
    public static function getNativeHooksForVirtualHook(string $virtualHook): array
120
    {
121 30
        return array_keys(
122 30
            array_filter(
123 30
                self::$virtualHookTriggers,
124 30
                function ($e) use ($virtualHook) {
125 30
                    return $e === $virtualHook;
126 30
                }
127 30
            )
128 30
        );
129
    }
130
131
    /**
132
     * Returns the argument placeholders for a given hook
133
     *
134
     * @param  string $hook
135
     * @return string
136
     */
137 7
    public static function getOriginalHookArguments(string $hook): string
138
    {
139 7
        static $arguments = [
140 7
            Hooks::COMMIT_MSG         => ' {$FILE}',
141 7
            Hooks::POST_MERGE         => ' {$SQUASH}',
142 7
            Hooks::PRE_COMMIT         => '',
143 7
            Hooks::POST_COMMIT        => '',
144 7
            Hooks::PRE_PUSH           => ' {$TARGET} {$URL}',
145 7
            Hooks::PREPARE_COMMIT_MSG => ' {$FILE} {$MODE} {$HASH}',
146 7
            Hooks::POST_CHECKOUT      => ' {$PREVIOUSHEAD} {$NEWHEAD} {$MODE}',
147 7
            Hooks::POST_REWRITE       => ' {$GIT-COMMAND}',
148 7
        ];
149
150 7
        return $arguments[$hook];
151
    }
152
153
    /**
154
     * Does a given hook allow for user input to be used
155
     *
156
     * @param  string $hook
157
     * @return bool
158
     */
159 8
    public static function receivesStdIn(string $hook): bool
160
    {
161 8
        return self::$hooksReceivingStdInput[$hook] ?? false;
162
    }
163
164
    /**
165
     * Tell if the given hook should trigger a virtual hook
166
     *
167
     * @param  string $hook
168
     * @return bool
169
     */
170 12
    public static function triggersVirtualHook(string $hook): bool
171
    {
172 12
        return isset(self::$virtualHookTriggers[$hook]);
173
    }
174
175
    /**
176
     * Return the virtual hook name that should be triggered by given hook
177
     *
178
     * @param  string $hook
179
     * @return string
180
     */
181 10
    public static function getVirtualHook(string $hook): string
182
    {
183 10
        if (!self::triggersVirtualHook($hook)) {
184 1
            throw new RuntimeException('no virtual hooks for ' . $hook);
185
        }
186 9
        return self::$virtualHookTriggers[$hook];
187
    }
188
}
189