Completed
Push — master ( 16d393...b8b112 )
by Sebastian
04:40
created

Beams::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 9.2
cc 2
eloc 15
nc 2
nop 4
crap 2
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\CaptainHook\Hook\Message\Action;
11
12
use SebastianFeldmann\CaptainHook\Config;
13
use SebastianFeldmann\CaptainHook\Console\IO;
14
use SebastianFeldmann\CaptainHook\Console\IOUtil;
15
use SebastianFeldmann\CaptainHook\Exception\ActionFailed;
16
use SebastianFeldmann\CaptainHook\Hook\Message\Rule;
17
use SebastianFeldmann\CaptainHook\Hook\Message\RuleBook;
18
use SebastianFeldmann\Cli\Output\Util as OutputUtil;
19
use SebastianFeldmann\Git\Repository;
20
21
/**
22
 * Class Beams
23
 *
24
 * @package CaptainHook
25
 * @author  Sebastian Feldmann <[email protected]>
26
 * @link    https://github.com/sebastianfeldmann/captainhook
27
 * @since   Class available since Release 0.9.0
28
 */
29
class Beams extends Book
30
{
31
    /**
32
     * Execute the configured action.
33
     *
34
     * @param  \SebastianFeldmann\CaptainHook\Config         $config
35
     * @param  \SebastianFeldmann\CaptainHook\Console\IO     $io
36
     * @param  \SebastianFeldmann\Git\Repository             $repository
37
     * @param  \SebastianFeldmann\CaptainHook\Config\Action  $action
38
     * @throws \Exception
39
     */
40 2
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
41
    {
42 2
        $options = $action->getOptions();
43 2
        $book    = new RuleBook();
44 2
        $book->setRules(
45
            [
46 2
                new Rule\CapitalizeSubject(),
47 2
                new Rule\LimitSubjectLength($options->get('subjectLength', 50)),
48 2
                new Rule\NoPeriodOnSubjectEnd(),
49 2
                new Rule\UseImperativeMood(),
50 2
                new Rule\LimitBodyLineLength($options->get('bodyLineLength', 72)),
51 2
                new Rule\SeparateSubjectFromBodyWithBlankLine(),
52
            ]
53
        );
54
55
        try {
56 2
            $this->validate($book, $repository);
57 1
        } catch (ActionFailed $exception) {
58 1
            $this->writeError($io, $repository);
59 1
            throw ActionFailed::fromPrevious($exception);
60
        }
61 1
    }
62
63
    /**
64
     * Write error to stdErr.
65
     *
66
     * @param \SebastianFeldmann\CaptainHook\Console\IO $io
67
     * @param \SebastianFeldmann\Git\Repository         $repository
68
     */
69 1
    private function writeError(IO $io, Repository $repository)
70
    {
71 1
        $io->writeError(array_merge(
72
            [
73 1
                '<error>COMMIT MESSAGE DOES NOT MEET THE REQUIREMENTS</error>',
74 1
                IOUtil::getLineSeparator(),
75
            ],
76 1
            OutputUtil::trimEmptyLines($repository->getCommitMsg()->getLines()),
77 1
            [IOUtil::getLineSeparator()]
78
        ));
79 1
    }
80
}
81