Cli::execute()   B
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
nc 16
nop 4
dl 0
loc 33
ccs 24
cts 24
cp 1
crap 6
rs 8.9617
c 2
b 0
f 0
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\Runner\Action;
13
14
use CaptainHook\App\Config;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Exception;
17
use CaptainHook\App\Runner\Action as ActionRunner;
18
use CaptainHook\App\Runner\Action\Cli\Command\Formatter;
19
use SebastianFeldmann\Cli\Processor\Symfony as Processor;
20
use SebastianFeldmann\Git\Repository;
21
22
/**
23
 * Class Cli
24
 *
25
 * @package CaptainHook
26
 * @author  Sebastian Feldmann <[email protected]>
27
 * @link    https://github.com/captainhook-git/captainhook
28
 * @since   Class available since Release 0.9.0
29
 * @internal
30
 */
31
class Cli implements ActionRunner
32
{
33
    /**
34
     * Execute the configured action
35
     *
36
     * @param \CaptainHook\App\Config           $config
37
     * @param \CaptainHook\App\Console\IO       $io
38
     * @param \SebastianFeldmann\Git\Repository $repository
39
     * @param \CaptainHook\App\Config\Action    $action
40
     * @return void
41
     * @throws \CaptainHook\App\Exception\ActionFailed
42
     */
43 19
    public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
44
    {
45 19
        $processor    = new Processor();
46 19
        $cmdOriginal  = $action->getAction();
47 19
        $cmdFormatted = $this->formatCommand($io, $config, $repository, $cmdOriginal);
48
49 19
        $io->write(
50 19
            '  <comment>cmd:</comment> ' . $cmdFormatted,
51 19
            true,
52 19
            IO::VERBOSE
53 19
        );
54
55 19
        $result = $processor->run($cmdFormatted);
56 19
        $output = '';
57
58 19
        if (!empty($result->getStdOut())) {
59 11
            $output .= PHP_EOL . $result->getStdOut();
60
        }
61 19
        if (!empty($result->getStdErr())) {
62 7
            $output .= PHP_EOL . $result->getStdErr();
63
        }
64
65 19
        if (!empty($output)) {
66 14
            $io->write(
67 14
                ['  <comment>command output:</comment>', trim($output)],
68 14
                true,
69 14
                !$result->isSuccessful() ? IO::NORMAL : IO::VERBOSE
70 14
            );
71
        }
72
73 19
        if (!$result->isSuccessful()) {
74 7
            throw new Exception\ActionFailed(
75 7
                'failed to execute: ' . $cmdFormatted
76 7
            );
77
        }
78
    }
79
80
    /**
81
     * Replace argument placeholder with their original values
82
     *
83
     * This replaces the hook argument and other placeholders
84
     *  - prepare-commit-msg => FILE, MODE, HASH
85
     *  - commit-msg         => FILE
86
     *  - pre-push           => TARGET, URL
87
     *  - pre-commit         => -
88
     *  - post-checkout      => PREVIOUSHEAD, NEWHEAD, MODE
89
     *  - post-merge         => SQUASH
90
     *
91
     * @param \CaptainHook\App\Console\IO       $io
92
     * @param \CaptainHook\App\Config           $config
93
     * @param \SebastianFeldmann\Git\Repository $repository
94
     * @param string                            $command
95
     * @return string
96
     */
97 19
    protected function formatCommand(IO $io, Config $config, Repository $repository, string $command): string
98
    {
99 19
        $formatter = new Formatter($io, $config, $repository);
100 19
        return $formatter->format($command);
101
    }
102
}
103