Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

Book::getErrorOutput()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 18
rs 9.9
ccs 7
cts 7
cp 1
cc 3
nc 4
nop 2
crap 3
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\Message\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Console\IOUtil;
17
use CaptainHook\App\Exception\ActionFailed;
18
use CaptainHook\App\Hook\Action;
19
use CaptainHook\App\Hook\Constrained;
20
use CaptainHook\App\Hook\Message\RuleBook;
21
use CaptainHook\App\Hook\Restriction;
22
use CaptainHook\App\Hooks;
23
use SebastianFeldmann\Cli\Output\Util as OutputUtil;
24
use SebastianFeldmann\Git\Repository;
25
26
/**
27
 * Class Book
28
 *
29
 * @package CaptainHook
30
 * @author  Sebastian Feldmann <[email protected]>
31
 * @link    https://github.com/captainhookphp/captainhook
32
 * @since   Class available since Release 0.9.0
33
 */
34
abstract class Book implements Action, Constrained
35
{
36
    /**
37
     * Returns a list of applicable hooks
38
     *
39
     * @return \CaptainHook\App\Hook\Restriction
40
     */
41
    public static function getRestriction(): Restriction
42
    {
43
        return Restriction::fromArray([Hooks::COMMIT_MSG]);
44
    }
45
46
    /**
47
     * Execute the configured action
48
     *
49
     * @param  \CaptainHook\App\Config           $config
50
     * @param  \CaptainHook\App\Console\IO       $io
51
     * @param  \SebastianFeldmann\Git\Repository $repository
52 6
     * @param  \CaptainHook\App\Config\Action    $action
53
     * @return void
54
     * @throws \Exception
55 6
     */
56 1
    abstract public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void;
57
58
    /**
59 5
     * Validate the message
60
     *
61 5
     * @param  \CaptainHook\App\Hook\Message\RuleBook $ruleBook
62 2
     * @param  \SebastianFeldmann\Git\Repository      $repository
63 2
     * @param  \CaptainHook\App\Console\IO            $io
64
     * @return void
65 3
     * @throws \CaptainHook\App\Exception\ActionFailed
66 3
     */
67
    protected function validate(RuleBook $ruleBook, Repository $repository, IO $io): void
68
    {
69
        // if this is a merge commit skip enforcing message rules
70
        if ($repository->isMerging()) {
71
            return;
72
        }
73
74
        $problems = $ruleBook->validate($repository->getCommitMsg());
75 2
76
        if (count($problems)) {
77 2
            $io->writeError($this->getErrorOutput($problems, $repository));
78
            throw new ActionFailed('Commit message validation failed');
79 2
        }
80 2
        $io->write('<info>All rules passed</info>');
81 2
    }
82
83 2
    /**
84
     * Format the error output
85 2
     *
86 2
     * @param  array<string>                     $problems
87 2
     * @param  \SebastianFeldmann\Git\Repository $repository
88
     * @return string
89
     */
90 2
    private function getErrorOutput(array $problems, Repository $repository): string
91
    {
92 2
        $err  = count($problems);
93
        $head = [
94
            IOUtil::getLineSeparator(80, '-'),
95
            'CAPTAINHOOK FOUND ' . $err . ' PROBLEM' . ($err === 1 ? '' : 'S') . ' IN YOUR COMMIT MESSAGE',
96
            IOUtil::getLineSeparator(80, '-')
97
        ];
98
        $msg  = OutputUtil::trimEmptyLines($repository->getCommitMsg()->getLines());
99
100
        $lines = [IOUtil::getLineSeparator(80, '-')];
101 2
        foreach ($problems as $problem) {
102
            $lines[] = '  ' . $this->formatProblem($problem);
103 2
        }
104 2
105
        $lines[] = IOUtil::getLineSeparator(80, '-');
106 2
107 1
        return implode(PHP_EOL, array_merge($head, $msg, $lines));
108
    }
109
110 2
    /**
111
     * Indent multi line problems so the lines after the first one are indented for better readability
112
     *
113
     * @param  string $problem
114
     * @return string
115
     */
116
    private function formatProblem(string $problem): string
117
    {
118
        $lines  = explode(PHP_EOL, $problem);
119
        $amount = count($lines);
120
121
        for ($i = 1; $i < $amount; $i++) {
122
            $lines[$i] = '    ' . $lines[$i];
123
        }
124
125
        return implode(PHP_EOL, $lines);
126
    }
127
}
128