Test Failed
Push — master ( dbca4e...c73643 )
by Jeroen De
04:56
created

GitHubParserHook::handle()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 26
rs 8.8571
cc 2
eloc 17
nc 2
nop 2
1
<?php
2
3
namespace GitHub;
4
5
use ParamProcessor\ProcessingResult;
6
use Parser;
7
use ParserHooks\HookHandler;
8
9
/**
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class GitHubParserHook implements HookHandler {
14
15
	private $gitHubFetcher;
16
17
	public function __construct( GitHubFetcher $gitHubFetcher ) {
18
		$this->gitHubFetcher = $gitHubFetcher;
19
	}
20
21
	public function handle( Parser $parser, ProcessingResult $result ): string {
22
		$params = $result->getParameters();
23
24
		$content = $this->gitHubFetcher->getFileContent(
25
			$params['repo']->getValue(),
26
			$params['branch']->getValue(),
27
			$params['file']->getValue()
28
		);
29
30
		if ( $params['lang']->getValue() === '' ) {
31
			return ( new NormalRenderer() )->getRenderedContent( $content, $params['file']->getValue() );
32
		}
33
34
		$syntaxRenderer = new SyntaxRenderer(
35
			function( string $syntaxContent ) use ( $parser ) {
36
				return $parser->recursiveTagParse( $syntaxContent, null );
37
			},
38
			$params['lang']->getValue(),
39
			$params['line']->getValue(),
40
			$params['start']->getValue(),
41
			$params['highlight']->getValue(),
42
			$params['inline']->getValue()
43
		);
44
45
		return $syntaxRenderer->getRenderedContent( $content );
46
	}
47
48
}
49