Completed
Push — master ( e7dd0e...dfe2c9 )
by Tonina
28:20
created

JsonStringReader::isJsonEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace WMDE\Fundraising\Frontend\Infrastructure;
6
7
use FileFetcher\FileFetcher;
8
9
class JsonStringReader {
10
11
	private $file;
12
	private $fileFetcher;
13
	private $json;
14
15 4
	public function __construct( string $file, FileFetcher $fileFetcher ) {
16 4
		$this->file = $file;
17 4
		$this->fileFetcher = $fileFetcher;
18 4
	}
19
20 4
	private function getJsonFile(): string {
21 4
		return $this->fileFetcher->fetchFile( $this->file );
22
	}
23
24 3
	private function isJsonEmpty(): bool {
25 3
		return $this->json === '';
26
	}
27
28 2
	private function isJsonValid(): bool {
29 2
		json_decode( $this->json );
30 2
		return json_last_error() === JSON_ERROR_NONE;
31
	}
32
33 4
	public function readAndValidateJson(): string {
34 4
		$this->json = $this->getJsonFile();
35 3
		if ( $this->isJsonEmpty() || !$this->isJsonValid() ) {
36 2
			throw new \RuntimeException( 'error_invalid_json' );
37
		}
38 1
		return $this->json;
39
	}
40
}