Passed
Push — master ( e9ff94...d484e5 )
by Jeroen De
04:40 queued 02:31
created

GitHubFetcher::getFileContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 3
crap 3
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 2
	public function __construct( FileFetcher $fileFetcher, string $gitHubUrl, array $repositoryWhitelist ) {
24 2
		$this->fileFetcher = $fileFetcher;
25 2
		$this->gitHubUrl = $gitHubUrl;
26 2
		$this->repositoryWhitelist = $repositoryWhitelist;
27 2
	}
28
29 2
	public function getFileContent( string $repoName, string $branchName, string $fileName ): string {
30 2
		if ( !$this->repoIsAllowed( $repoName ) ) {
31 1
			return '';
32
		}
33
34 1
		$url = $this->getFileUrl( $repoName, $branchName, $fileName );
35
36
		try {
37 1
			return $this->fileFetcher->fetchFile( $url );
38
		}
39 1
		catch ( FileFetchingException $ex ) {
40 1
			return '';
41
		}
42
	}
43
44 2
	private function repoIsAllowed( string $repoName ): bool {
45 2
		return $this->repositoryWhitelist === []
46 2
			|| in_array( $repoName, $this->repositoryWhitelist );
47
	}
48
49 1
	private function getFileUrl( string $repoName, string $branchName, string $fileName ): string {
50 1
		return sprintf(
51 1
			'%s/%s/%s/%s',
52 1
			$this->gitHubUrl,
53 1
			$repoName,
54 1
			$branchName,
55 1
			$fileName
56
		);
57
	}
58
59
}
60