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

GitHubFetcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 48
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFileContent() 0 14 3
A repoIsAllowed() 0 4 2
A getFileUrl() 0 9 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 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