Test Failed
Push — master ( c73643...f19b3f )
by Jeroen De
07:49
created

GitHubFetcher::repoIsAllowed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace GitHub;
4
5
use FileFetcher\FileFetcher;
6
use FileFetcher\FileFetchingException;
7
8
/**
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class GitHubFetcher {
13
14
	private $fileFetcher;
15
	private $gitHubUrl;
16
	private $repositoryWhitelist;
17
18
	/**
19
	 * @param FileFetcher $fileFetcher
20
	 * @param string $gitHubUrl
21
	 * @param string[] $repositoryWhitelist Empty for no restrictions
22
	 */
23
	public function __construct( FileFetcher $fileFetcher, string $gitHubUrl, array $repositoryWhitelist ) {
24
		$this->fileFetcher = $fileFetcher;
25
		$this->gitHubUrl = $gitHubUrl;
26
		$this->repositoryWhitelist = $repositoryWhitelist;
27
	}
28
29
	public function getFileContent( string $repoName, string $branchName, string $fileName ): string {
30
		if ( !$this->repoIsAllowed( $repoName ) ) {
31
			return '';
32
		}
33
34
		$url = $this->getFileUrl( $repoName, $branchName, $fileName );
35
36
		try {
37
			return $this->fileFetcher->fetchFile( $url );
38
		}
39
		catch ( FileFetchingException $ex ) {
40
			return '';
41
		}
42
	}
43
44
	private function repoIsAllowed( string $repoName ): bool {
45
		return $this->repositoryWhitelist === []
46
			|| in_array( $repoName, $this->repositoryWhitelist );
47
	}
48
49
	private function getFileUrl( string $repoName, string $branchName, string $fileName ): string {
50
		return sprintf(
51
			'%s/%s/%s/%s',
52
			$this->gitHubUrl,
53
			$repoName,
54
			$branchName,
55
			$fileName
56
		);
57
	}
58
59
}
60