PrepareFromFile   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 13
c 3
b 0
f 0
dl 0
loc 34
ccs 14
cts 14
cp 1
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 22 5
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\Hook\Message\Action;
15
16
use CaptainHook\App\Config;
17
use CaptainHook\App\Console\IO;
18
use CaptainHook\App\Exception\ActionFailed;
19
use CaptainHook\App\Hook\Action;
20
use SebastianFeldmann\Git\CommitMessage;
21
use SebastianFeldmann\Git\Repository;
22
23
/**
24
 * Class PrepareFromFile
25
 *
26
 * Example configuration:
27
 * {
28
 *   "action": "\\CaptainHook\\App\\Hook\\Message\\Action\\PrepareFromFile"
29
 *   "options": {
30
 *     "file": ".git/CH_MSG_CACHE"
31
 *   }
32
 * }
33
 *
34
 * @package CaptainHook
35
 * @author  Sebastian Feldmann <[email protected]>
36
 * @link    https://github.com/captainhook-git/captainhook
37
 * @since   Class available since Release 5.11.0
38
 */
39
class PrepareFromFile implements Action
40
{
41
    /**
42
     * Execute the configured action
43
     *
44
     * @param  \CaptainHook\App\Config           $config
45
     * @param  \CaptainHook\App\Console\IO       $io
46
     * @param  \SebastianFeldmann\Git\Repository $repository
47
     * @param  \CaptainHook\App\Config\Action    $action
48
     * @return void
49
     * @throws \Exception
50
     */
51 3
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
52
    {
53 3
        $options   = $action->getOptions();
54 3
        $cacheFile = $repository->getRoot() . '/' . $options->get('file', '');
55 3
        if (empty($options->get('file', ''))) {
56 1
            throw new ActionFailed('PrepareFromFile requires \'file\' option');
57
        }
58
59 2
        if (!is_file($cacheFile)) {
60 1
            return;
61
        }
62
63
        // if there is a commit message don't do anything just delete the file
64 1
        if ($repository->getCommitMsg()->isEmpty()) {
65 1
            $msg = (string)file_get_contents($cacheFile);
66 1
            $repository->setCommitMsg(
67 1
                new CommitMessage($msg, $repository->getCommitMsg()->getCommentCharacter())
68 1
            );
69
        }
70
71 1
        if (!$options->get('keep', false)) {
72 1
            unlink($cacheFile);
73
        }
74
    }
75
}
76