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

InMemoryFileFetcher::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 3
nop 1
crap 4.074
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