Test Failed
Push — extract ( e8a5db...d01209 )
by Jeroen De
09:33
created

GitHubParserHook   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 14
c 3
b 1
f 0
lcom 1
cbo 3
dl 0
loc 93
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 14 1
A setFields() 0 7 1
C getRenderedContent() 0 33 7
A isMarkdownFile() 0 4 2
A fileHasExtension() 0 4 1
A renderAsMarkdown() 0 3 1
1
<?php
2
3
namespace GitHub;
4
5
use FileFetcher\FileFetcher;
6
use FileFetcher\FileFetchingException;
7
use Michelf\Markdown;
8
use ParamProcessor\ProcessingResult;
9
use Parser;
10
use ParserHooks\HookHandler;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class GitHubParserHook implements HookHandler {
17
18
	private $gitHubFetcher;
19
20
	/**
21
	 * @var Parser
22
	 */
23
	private $parser;
24
25
	// Parameters for SyntaxHighlight extension (formerly SyntaxHighlight_GeSHi)
26
	// https://www.mediawiki.org/wiki/Extension:SyntaxHighlight
27
	private $syntaxHighlightLanguage;
28
	private $syntaxHighlightEnableLineNumbers;
29
	private $syntaxHighlightStartingLineNumber;
30
	private $syntaxHighlightHighlightedLines;
31
	private $syntaxHighlightInlineSource;
32
33
	public function __construct( GitHubFetcher $gitHubFetcher ) {
34
		$this->gitHubFetcher = $gitHubFetcher;
35
	}
36
37
	public function handle( Parser $parser, ProcessingResult $result ): string {
38
		$this->parser = $parser;
39
40
		$params = $result->getParameters();
41
		$this->setFields( $params );
42
43
		$content = $this->gitHubFetcher->getFileContent(
44
			$params['repo']->getValue(),
45
			$params['branch']->getValue(),
46
			$params['file']->getValue()
47
		);
48
49
		return $this->getRenderedContent( $content, $params['file']->getValue() );
50
	}
51
52
	private function setFields( array $params ) {
53
		$this->syntaxHighlightLanguage = $params['lang']->getValue();
54
		$this->syntaxHighlightEnableLineNumbers = $params['line']->getValue();
55
		$this->syntaxHighlightStartingLineNumber = $params['start']->getValue();
56
		$this->syntaxHighlightHighlightedLines = $params['highlight']->getValue();
57
		$this->syntaxHighlightInlineSource = $params['inline']->getValue();
58
	}
59
60
	private function getRenderedContent( string $content, string $fileName ): string {
61
		if ( $this->syntaxHighlightLanguage === '' ) {
62
			if ( $this->isMarkdownFile( $fileName ) ) {
63
				return $this->renderAsMarkdown( $content );
64
			}
65
66
			return $content;
67
		}
68
69
		$syntax_highlight = "<syntaxhighlight lang=\"". $this->syntaxHighlightLanguage ."\"";
70
		$syntax_highlight .= " start=\"". $this->syntaxHighlightStartingLineNumber ."\"";
71
72
		if ( $this->syntaxHighlightEnableLineNumbers === true ) {
73
			$syntax_highlight .= " line";
74
		}
75
76
		if ( $this->syntaxHighlightHighlightedLines !== "" ) {
77
			$syntax_highlight .= " highlight=\"". $this->syntaxHighlightHighlightedLines ."\"";
78
		}
79
80
		if ( $this->syntaxHighlightInlineSource === true ) {
81
			$syntax_highlight .= " inline";
82
		}
83
84
		$syntax_highlight .= ">$content</syntaxhighlight>";
85
		$parsed = $this->parser->recursiveTagParse( $syntax_highlight, null );
86
87
		if ( is_string( $parsed ) ) {
88
			return $parsed;
89
		}
90
91
		return '';
92
	}
93
94
	private function isMarkdownFile( string $fileName ): bool {
95
		return $this->fileHasExtension( $fileName, 'md' )
96
			   || $this->fileHasExtension( $fileName,'markdown' );
97
	}
98
99
	private function fileHasExtension( string $fileName, string $extension ): bool {
100
		$fullExtension = '.' . $extension;
101
		return substr( $fileName, -strlen( $fullExtension ) ) === $fullExtension;
102
	}
103
104
	private function renderAsMarkdown( string $content ): string {
105
		return Markdown::defaultTransform( $content );
106
	}
107
108
}
109