Completed
Push — master ( b3520b...1dcb52 )
by Jeroen De
03:36
created

PsrCacheFileFetcher::createCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace FileFetcher;
6
7
use Psr\SimpleCache\CacheException;
8
use Psr\SimpleCache\CacheInterface;
9
10
/**
11
 * Decorator that caches files using psr/simple-cache.
12
 * https://packagist.org/packages/psr/simple-cache
13
 *
14
 * @since 5.0
15
 *
16
 * @licence BSD-3-Clause
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class PsrCacheFileFetcher implements FileFetcher {
20
21
	private $fileFetcher;
22
	private $cache;
23
	private $keyBuilder;
24
25
	/**
26
	 * @param FileFetcher $fileFetcher
27
	 * @param CacheInterface $cache
28
	 * @param callable|null $keyBuilderFunction Gets the fileUrl (string) and needs to return a valid cache key (string)
29
	 */
30 10
	public function __construct( FileFetcher $fileFetcher, CacheInterface $cache, callable $keyBuilderFunction = null ) {
31 10
		$this->fileFetcher = $fileFetcher;
32 10
		$this->cache = $cache;
33 10
		$this->keyBuilder = $keyBuilderFunction ?? $this->getDefaultKeyBuilder();
34 10
	}
35
36 4
	private function getDefaultKeyBuilder(): callable {
37
		return function( string $fileUrl ): string {
38 4
			return preg_replace(
39 4
					'/[^A-Za-z0-9\-]/',
40 4
					'_',
41 4
					$fileUrl
42
				)
43 4
				. '-' . substr( sha1( $fileUrl ), 0, 5 );
44 4
		};
45
	}
46
47
	/**
48
	 * @see FileFetcher::fetchFile
49
	 * @throws FileFetchingException
50
	 */
51 10
	public function fetchFile( string $fileUrl ): string {
52 10
		$fileContents = $this->getFileContentsFromCache( $fileUrl );
53
54 10
		if ( $fileContents === null ) {
55 9
			return $this->retrieveAndCacheFile( $fileUrl );
56
		}
57
58 1
		return $fileContents;
59
	}
60
61 10
	private function getFileContentsFromCache( string $fileUrl ): ?string {
62
		try {
63 10
			return $this->cache->get( $this->createCacheKey( $fileUrl ) );
64
		}
65 1
		catch ( CacheException $ex ) {
0 ignored issues
show
Bug introduced by
The class Psr\SimpleCache\CacheException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
66 1
			return null;
67
		}
68
	}
69
70 10
	private function createCacheKey( string $fileUrl ): string {
71 10
		return ( $this->keyBuilder )( $fileUrl );
72
	}
73
74 9
	private function retrieveAndCacheFile( $fileUrl ): string {
75 9
		$fileContents = $this->fileFetcher->fetchFile( $fileUrl );
76
77
		try {
78 8
			$this->cache->set( $this->createCacheKey( $fileUrl ), $fileContents );
79
		}
80 1
		catch ( CacheException $ex ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class Psr\SimpleCache\CacheException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
81
		}
82
83 8
		return $fileContents;
84
	}
85
86
}
87