Completed
Push — master ( aa9657...bb9832 )
by Sebastian
05:21
created

Hook::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 10
nc 3
nop 0
crap 3
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\Runner;
11
12
use SebastianFeldmann\CaptainHook\Hook\Action as ActionInterface;
13
14
/**
15
 *  Hook
16
 *
17
 * @package CaptainHook
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/sebastianfeldmann/captainhook
20
 * @since   Class available since Release 0.9.0
21
 */
22
class Hook extends HookHandler
23
{
24
    /**
25
     * Hook config
26
     *
27
     * @var \SebastianFeldmann\CaptainHook\Config\Hook
28
     */
29
    private $hookConfig;
30
31
    /**
32 7
     * Execute installation.
33
     */
34
    public function run()
35 7
    {
36
        /** @var \SebastianFeldmann\CaptainHook\Config\Hook $hookConfig */
37
        $this->hookConfig = $this->config->getHookConfig($this->hookToHandle);
38 7
39 6
        // execute hooks only if hook is enabled in captainhook.json
40 6
        if ($this->hookConfig->isEnabled()) {
41 1
            $this->io->write('<info>execute hook:</info> <comment>' . $this->hookToHandle . '</comment>');
42 1
            foreach ($this->getActionsToRun() as $action) {
43 6
                $this->io->write([str_repeat('#', 80), '# <comment>' . $action->getAction() . '</comment>', '']);
44
                $runner = $this->getActionRunner($action->getType());
45
                $runner->execute($this->config, $this->io, $this->repository, $action);
46 1
            }
47
        } else {
48 7
            $this->io->write('<info>skip hook:</info> <comment>' . $this->hookToHandle . '</comment>');
49
        }
50
    }
51
52
    /**
53
     * Return list of actions to run.
54
     *
55 6
     * @return \SebastianFeldmann\CaptainHook\Config\Action[]
56
     */
57 6
    protected function getActionsToRun() : array
58
    {
59
        return $this->hookConfig->getActions();
60
    }
61
62
    /**
63
     * Return matching action runner.
64
     *
65
     * @param  string $type
66
     * @return \SebastianFeldmann\CaptainHook\Hook\Action
67 3
     * @throws \RuntimeException
68
     */
69
    public function getActionRunner($type) : ActionInterface
70 3
    {
71 1
        switch ($type) {
72 3
            case 'php':
73 2
                return new Action\PHP();
74
            case 'cli':
75 1
                return new Action\Cli();
76
            default:
77
                throw new \RuntimeException('Unknown action type: ' . $type);
78
        }
79
    }
80
}
81