Passed
Push — master ( 80395d...915f9b )
by Jeroen De
01:15
created

StopwatchFileFetcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare( strict_types=1 );
4
5
namespace FileFetcher;
6
7
use Symfony\Component\Stopwatch\Stopwatch;
8
9
/**
10
 * Decorator for FileFetcher objects that profiles file fetching calls using Symfony Stopwatch.
11
 * https://packagist.org/packages/symfony/stopwatch
12
 *
13
 * This class depends on Symfony Stopwatch and can thus only be used when you have symfony/stopwatch loaded.
14
 *
15
 * @since 4.6
16
 *
17
 * @licence BSD-3-Clause
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class StopwatchFileFetcher implements FileFetcher {
21
22
	public const STOPWATCH_CATEGORY = 'file_fetcher';
23
24
	private $fileFetcher;
25
	private $stopwatch;
26
	private $category;
27
28 5
	public function __construct( FileFetcher $fileFetcher, Stopwatch $stopwatch, string $category = self::STOPWATCH_CATEGORY ) {
29 5
		$this->fileFetcher = $fileFetcher;
30 5
		$this->stopwatch = $stopwatch;
31 5
		$this->category = $category;
32 5
	}
33
34 5
	public function fetchFile( string $fileUrl ): string {
35 5
		$this->stopwatch->start( $fileUrl, $this->category );
36
37
		try {
38 5
			$fileContent = $this->fileFetcher->fetchFile( $fileUrl );
39
		}
40 1
		catch ( FileFetchingException $ex ) {
41 1
			throw $ex;
42
		}
43 4
		finally {
44 5
			$this->stopwatch->stop( $fileUrl );
45
		}
46
47 4
		return $fileContent;
48
	}
49
50
}
51