Completed
Push — master ( 4405cb...f3f48a )
by Sebastian
02:04
created

Hook::formatHookHeadline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 SebastianFeldmann\Git\Repository;
17
18
/**
19
 * Hook
20
 *
21
 * @package CaptainHook
22
 * @author  Sebastian Feldmann <[email protected]>
23
 * @link    https://github.com/captainhookphp/captainhook
24
 * @since   Class available since Release 0.9.0
25
 */
26
abstract class Hook extends RepositoryAware
27
{
28
    /**
29
     * Hook that should be handled.
30
     *
31
     * @var string
32
     */
33
    protected $hook;
34
35
    /**
36
     * List of original hook arguments
37
     *
38
     * @var \CaptainHook\App\Config\Options
39
     */
40
    protected $arguments;
41
42
    /**
43
     * HookHandler constructor.
44
     *
45
     * @param \CaptainHook\App\Console\IO       $io
46
     * @param \CaptainHook\App\Config           $config
47
     * @param \SebastianFeldmann\Git\Repository $repository
48
     * @param \CaptainHook\App\Config\Options   $arguments
49
     */
50 20
    public function __construct(IO $io, Config $config, Repository $repository, Options $arguments)
51
    {
52 20
        parent::__construct($io, $config, $repository);
53 20
        $this->arguments = $arguments;
54 20
    }
55
56
    /**
57
     * Execute stuff before executing any actions
58
     *
59
     * @return void
60
     */
61 6
    public function beforeHook() : void
62
    {
63
        // empty template method
64 6
    }
65
66
    /**
67
     * Execute stuff before every actions
68
     *
69
     * @return void
70
     */
71 1
    public function beforeAction() : void
72
    {
73
        // empty template method
74 1
    }
75
76
    /**
77
     * Execute stuff after every actions
78
     *
79
     * @return void
80
     */
81 1
    public function afterAction() : void
82
    {
83
        //empty template method
84 1
    }
85
86
    /**
87
     * Execute stuff after all actions
88
     *
89
     * @return void
90
     */
91 6
    public function afterHook() : void
92
    {
93
        // empty template method
94 6
    }
95
96
    /**
97
     * Execute the hook and all its actions
98
     *
99
     * @return void
100
     * @throws \Exception
101
     */
102 20
    public function run() : void
103
    {
104
        /** @var \CaptainHook\App\Config\Hook $hookConfig */
105 20
        $hookConfig = $this->config->getHookConfig($this->hook);
106 20
        $actions    = $hookConfig->getActions();
107
108
        // if hook is not enabled in captainhook configuration skip the execution
109 20
        if (!$hookConfig->isEnabled()) {
110 6
            $this->io->write($this->formatHookHeadline('Skip'));
111 6
            return;
112
        }
113
        // if no actions are configured do nothing
114 14
        if (count($actions) === 0) {
115 6
            $this->io->write(['', '<info>No actions to execute</info>']);
116 6
            return;
117
        }
118
119 8
        $this->io->write($this->formatHookHeadline('Execute'));
120 8
        $this->beforeHook();
121 6
        foreach ($actions as $action) {
122 6
            $this->handleAction($action);
123
        }
124 6
        $this->afterHook();
125 6
    }
126
127
    /**
128
     * Executes a configured hook action
129
     *
130
     * @param  \CaptainHook\App\Config\Action $action
131
     * @return void
132
     * @throws \Exception
133
     */
134 6
    protected function handleAction(Config\Action $action) : void
135
    {
136 6
        $this->io->write(['', 'Action: <comment>' . $action->getAction() . '</comment>']);
137
138 6
        $execMethod = self::getExecMethod($action->getType());
139 6
        $this->{$execMethod}($action);
140 6
    }
141
142
    /**
143
     * Execute a php hook action
144
     *
145
     * @param  \CaptainHook\App\Config\Action $action
146
     * @return void
147
     * @throws \CaptainHook\App\Exception\ActionFailed
148
     */
149 1
    protected function executePhpAction(Config\Action $action) : void
150
    {
151 1
        $this->beforeAction();
152 1
        $runner = new Action\PHP();
153 1
        $runner->execute($this->config, $this->io, $this->repository, $action);
154 1
        $this->afterAction();
155 1
    }
156
157
    /**
158
     * Execute a cli hook action
159
     *
160
     * @param  \CaptainHook\App\Config\Action $action
161
     * @return void
162
     * @throws \CaptainHook\App\Exception\ActionFailed
163
     */
164 5
    protected function executeCliAction(Config\Action $action) : void
165
    {
166
        // since the cli has no straight way to communicate back to php
167
        // cli hooks have to handle sync stuff by them self
168
        // so no 'beforeAction' or 'afterAction' is called here
169 5
        $runner = new Action\Cli();
170 5
        $runner->execute($this->io, $action, $this->arguments);
171 5
    }
172
173
    /**
174
     * Return the right method name to execute an action
175
     *
176
     * @param  string $type
177
     * @return string
178
     */
179 8
    public static function getExecMethod(string $type) : string
180
    {
181 8
        $valid = ['php' => 'executePhpAction', 'cli' => 'executeCliAction'];
182
183 8
        if (!isset($valid[$type])) {
184 1
            throw new \RuntimeException('invalid action type: ' . $type);
185
        }
186 7
        return $valid[$type];
187
    }
188
189
    /**
190
     * Some fancy output formatting
191
     *
192
     * @param  string $mode
193
     * @return array
194
     */
195 14
    private function formatHookHeadline(string $mode) : array
196
    {
197 14
        $headline = ' ' . $mode . ' hook: <comment>' . $this->hook . '</comment> ';
198
        return [
199 14
            '',
200 14
            IOUtil::getLineSeparator(8) .
201 14
            $headline .
202 14
            IOUtil::getLineSeparator(80 - 8 - strlen(strip_tags($headline)))
203
        ];
204
    }
205
}
206