Completed
Push — master ( dde524...92e3a2 )
by Sebastian
06:05
created

Hook::executeAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 0
cp 0
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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 CaptainHook\App\Runner;
11
12
use CaptainHook\App\Config;
13
use CaptainHook\App\Config\Options;
14
use CaptainHook\App\Console\IO;
15
use CaptainHook\App\Console\IOUtil;
16
use CaptainHook\App\Hook\Action as ActionInterface;
17
use SebastianFeldmann\Git\Repository;
18
19
/**
20
 * Hook
21
 *
22
 * @package CaptainHook
23
 * @author  Sebastian Feldmann <[email protected]>
24
 * @link    https://github.com/sebastianfeldmann/captainhook
25
 * @since   Class available since Release 0.9.0
26
 */
27
abstract class Hook extends RepositoryAware
28
{
29
    /**
30
     * Hook that should be handled.
31
     *
32
     * @var string
33
     */
34
    protected $hookName;
35 8
36
    /**
37
     * List of original hook arguments
38 8
     *
39
     * @var \CaptainHook\App\Config\Options
40
     */
41 8
    protected $arguments;
42 7
43 7
    /**
44 1
     * HookHandler constructor.
45 1
     *
46 1
     * @param \CaptainHook\App\Console\IO       $io
47 1
     * @param \CaptainHook\App\Config           $config
48
     * @param \SebastianFeldmann\Git\Repository $repository
49 1
     * @param \CaptainHook\App\Config\Options   $arguments
50 1
     */
51 7
    public function __construct(IO $io, Config $config, Repository $repository, Options $arguments)
52
    {
53
        parent::__construct($io, $config, $repository);
54 1
        $this->arguments = $arguments;
55
    }
56 8
57
    /**
58
     * Execute stuff before executing any actions
59
     *
60
     * @return void
61
     */
62
    public function beforeHook()
63 7
    {
64
        // empty template method
65 7
    }
66
67
    /**
68
     * Execute stuff before every actions
69
     *
70
     * @return void
71
     */
72
    public function beforeAction()
73
    {
74
        // empty template method
75 3
    }
76
77
    /**
78 3
     * Execute stuff after every actions
79 1
     *
80 3
     * @return void
81 2
     */
82
    public function afterAction()
83 1
    {
84
        //empty template method
85
    }
86
87
    /**
88
     * Execute stuff after all actions
89
     *
90
     * @return void
91
     */
92
    public function afterHook()
93
    {
94
        // empty template method
95
    }
96
97
    /**
98
     * Execute installation.
99
     *
100
     * @throws \Exception
101
     */
102
    public function run()
103
    {
104
        $this->beforeHook();
105
        /** @var \CaptainHook\App\Config\Hook $hookConfig */
106
        $hookConfig = $this->config->getHookConfig($this->hookName);
107
108
        // if hook is not enabled in captainhook.json skip action execution
109
        if (!$hookConfig->isEnabled()) {
110
            $this->io->write('<info>skip hook:</info> <comment>' . $this->hookName . '</comment>');
111
            return;
112
        }
113
114
        $this->io->write(['', '<info>execute hook:</info> <comment>' . $this->hookName . '</comment>']);
115
        foreach ($hookConfig->getActions() as $action) {
116
            $this->executeAction($action);
117
        }
118
        $this->afterHook();
119
    }
120
121
    /**
122
     * Executes a configured hook action
123
     *
124
     * @param  \CaptainHook\App\Config\Action $action
125
     * @throws \Exception
126
     */
127
    protected function executeAction(Config\Action $action)
128
    {
129
        $this->io->write([
130
            '',
131
            'Action: <comment>' . $action->getAction() . '</comment>',
132
            IOUtil::getLineSeparator()
133
        ]);
134
135
        $type   = $action->getType();
136
        $runner = self::getActionRunner($type);
137
138
        $this->beforeAction();
139
        $runner->execute($this->config, $this->io, $this->repository, $action);
140
141
        // execute post handling only for php actions
142
        // cli actions should do post actions them self
143
        if ($type === 'php') {
144
            $this->afterAction();
145
        }
146
147
        $this->io->write([str_repeat('-', 80)]);
148
    }
149
150
    /**
151
     * Return matching action runner.
152
     *
153
     * @param  string $type
154
     * @return \CaptainHook\App\Hook\Action
155
     * @throws \RuntimeException
156
     */
157
    public static function getActionRunner($type) : ActionInterface
158
    {
159
        switch ($type) {
160
            case 'php':
161
                return new Action\PHP();
162
            case 'cli':
163
                return new Action\Cli();
164
            default:
165
                throw new \RuntimeException('Unknown action type: ' . $type);
166
        }
167
    }
168
}
169