AskConfirmation   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 37
ccs 6
cts 6
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 4 3
A __construct() 0 4 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
namespace CaptainHook\App\Hook\UserInput\EventHandler;
13
14
use CaptainHook\App\Console\IOUtil;
15
use CaptainHook\App\Event;
16
use CaptainHook\App\Event\Handler;
17
use CaptainHook\App\Exception\ActionFailed;
18
19
/**
20
 * Writes to commit message cache file to load it for a later commit
21
 *
22
 * @package CaptainHook
23
 * @author  Sebastian Feldmann <[email protected]>
24
 * @link    https://github.com/captainhook-git/captainhook
25
 * @since   Class available since Release 5.11.0
26
 */
27
class AskConfirmation implements Handler
28
{
29
    /**
30
     * Question to ask
31
     *
32
     * @var string
33
     */
34
    private string $question;
35
36
    /**
37
     * No input ok or not
38
     *
39
     * @var bool
40
     */
41
    private bool $default;
42
43
    /**
44
     * @param string $question
45
     * @param bool   $default
46
     */
47 3
    public function __construct(string $question, bool $default = false)
48
    {
49 3
        $this->question = $question;
50 3
        $this->default  = $default;
51
    }
52
53
    /**
54
     * Writes the commit message to a cache file to reuse it for the next commit
55
     *
56
     * @param \CaptainHook\App\Event $event
57
     * @return void
58
     * @throws \CaptainHook\App\Exception\ActionFailed
59
     */
60 2
    public function handle(Event $event): void
61
    {
62 2
        if (!IOUtil::answerToBool($event->io()->ask(PHP_EOL .  $this->question . ' ', $this->default ? 'y' : 'n'))) {
63 1
            throw new ActionFailed('no confirmation, abort!');
64
        }
65
    }
66
}
67