Completed
Push — php7 ( 52fd67 )
by Jeroen De
04:02
created

InMemoryFileFetcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 31
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchFile() 0 7 2
A __construct() 0 9 4
1
<?php
2
3
declare( strict_types=1 );
4
5
namespace FileFetcher;
6
7
use InvalidArgumentException;
8
9
/**
10
 * @since 3.1
11
 *
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class InMemoryFileFetcher implements FileFetcher {
16
17
	private $files;
18
19
	/**
20
	 * @param string[] $files
21
	 * @throws InvalidArgumentException
22
	 */
23 3
	public function __construct( array $files ) {
24 3
		foreach ( $files as $url => $fileContents ) {
25 2
			if ( !is_string( $url ) || !is_string( $fileContents ) ) {
26
				throw new InvalidArgumentException( 'Both file url and file contents need to be of type string' );
27
			}
28
		}
29
30 3
		$this->files = $files;
31 3
	}
32
33
	/**
34
	 * @see FileFetcher::fetchFile
35
	 * @throws FileFetchingException
36
	 */
37 3
	public function fetchFile( string $fileUrl ): string {
38 3
		if ( array_key_exists( $fileUrl, $this->files ) ) {
39 1
			return $this->files[$fileUrl];
40
		}
41
42 2
		throw new FileFetchingException( $fileUrl );
43
	}
44
45
}
46