Completed
Push — master ( 562528...e703ac )
by Sebastian
02:26
created

Beams::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 11
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\RuleBook;
17
use SebastianFeldmann\Cli\Output\Util as OutputUtil;
18
use SebastianFeldmann\Git\Repository;
19
20
/**
21
 * Class Beams
22
 *
23
 * @package CaptainHook
24
 * @author  Sebastian Feldmann <[email protected]>
25
 * @link    https://github.com/sebastianfeldmann/captainhook
26
 * @since   Class available since Release 0.9.0
27
 */
28
class Beams extends Book
29
{
30
    /**
31
     * Execute the configured action.
32
     *
33
     * @param  \SebastianFeldmann\CaptainHook\Config         $config
34
     * @param  \SebastianFeldmann\CaptainHook\Console\IO     $io
35
     * @param  \SebastianFeldmann\Git\Repository             $repository
36
     * @param  \SebastianFeldmann\CaptainHook\Config\Action  $action
37
     * @throws \Exception
38
     */
39 2
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action)
40
    {
41 2
        $options = $action->getOptions();
42 2
        $book    = new RuleBook();
43 2
        $book->setRules(RuleBook\RuleSet::beams(
44 2
            $options->get('subjectLength', 50),
45 2
            $options->get('bodyLineLength', 72)
46
        ));
47
48
        try {
49 2
            $this->validate($book, $repository);
50 1
        } catch (ActionFailed $exception) {
51 1
            $this->writeError($io, $repository);
52 1
            throw ActionFailed::fromPrevious($exception);
53
        }
54 1
    }
55
56
    /**
57
     * Write error to stdErr.
58
     *
59
     * @param \SebastianFeldmann\CaptainHook\Console\IO $io
60
     * @param \SebastianFeldmann\Git\Repository         $repository
61
     */
62 1
    private function writeError(IO $io, Repository $repository)
63
    {
64 1
        $io->writeError(array_merge(
65
            [
66 1
                '<error>COMMIT MESSAGE DOES NOT MEET THE REQUIREMENTS</error>',
67 1
                IOUtil::getLineSeparator(),
68
            ],
69 1
            OutputUtil::trimEmptyLines($repository->getCommitMsg()->getLines()),
70 1
            [IOUtil::getLineSeparator()]
71
        ));
72 1
    }
73
}
74