Completed
Push — master ( 32dcda...951284 )
by
unknown
06:45 queued 10s
created

TermFallbackCacheFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 85
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getTermFallbackCache() 0 28 3
1
<?php
2
3
declare( strict_types = 1 );
4
namespace Wikibase\Lib;
5
6
use CachedBagOStuff;
7
use IBufferingStatsdDataFactory;
8
use Psr\Log\LoggerInterface;
9
use Psr\SimpleCache\CacheInterface;
10
use Wikibase\Lib\TermFallbackCache\TermFallbackCacheServiceFactory;
11
12
/**
13
 * Factory for accessing the shared cache
14
 *
15
 * @license GPL-2.0-or-later
16
 */
17
class TermFallbackCacheFactory {
18
19
	/**
20
	 * @var int|string
21
	 */
22
	private $termFallbackCacheType;
23
24
	/**
25
	 * @var LoggerInterface
26
	 */
27
	private $logger;
28
29
	/**
30
	 * @var string
31
	 */
32
	private $cacheSecret;
33
34
	/**
35
	 * @var IBufferingStatsdDataFactory
36
	 */
37
	private $statsdDataFactory;
38
39
	/**
40
	 * @var TermFallbackCacheServiceFactory
41
	 */
42
	private $serviceFactory;
43
44
	/**
45
	 * @var int|null
46
	 */
47
	private $cacheVersion;
48
49
	/**
50
	 * @param int|string $cacheType
51
	 * @param LoggerInterface $logger
52
	 * @param IBufferingStatsdDataFactory $statsdDataFactory
53
	 * @param string $cacheSecret
54
	 * @param TermFallbackCacheServiceFactory $serviceFactory
55
	 * @param int|null $cacheVersion
56
	 */
57
	public function __construct(
58
		$cacheType,
59
		LoggerInterface $logger,
60
		IBufferingStatsdDataFactory $statsdDataFactory,
61
		string $cacheSecret,
62
		TermFallbackCacheServiceFactory $serviceFactory,
63
		?int $cacheVersion
64
	) {
65
		$this->termFallbackCacheType = $cacheType;
66
		$this->logger = $logger;
67
		$this->statsdDataFactory = $statsdDataFactory;
68
		$this->cacheSecret = $cacheSecret;
69
		$this->serviceFactory = $serviceFactory;
70
		$this->cacheVersion = $cacheVersion;
71
	}
72
73
	public function getTermFallbackCache(): CacheInterface {
74
		$bagOStuff = $this->serviceFactory->newSharedCache( $this->termFallbackCacheType );
75
		if ( !$bagOStuff instanceof CachedBagOStuff ) {
0 ignored issues
show
Bug introduced by
The class CachedBagOStuff does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
76
			$bagOStuff = $this->serviceFactory->newInMemoryCache( $bagOStuff ); // wrap in an in-memory cache
77
		}
78
79
		$prefix = 'wikibase.repo.formatter.'; // intentionally shared between repo and client
80
		if ( $this->cacheVersion !== null ) {
81
			$prefix .= "$this->cacheVersion.";
82
		}
83
84
		$cache = $this->serviceFactory->newCache(
85
			$bagOStuff,
86
			$prefix,
87
			$this->cacheSecret
88
		);
89
90
		$cache->setLogger( $this->logger );
91
92
		return $this->serviceFactory->newStatsdRecordingCache(
93
			$cache,
94
			$this->statsdDataFactory,
95
			[
96
				'miss' => 'wikibase.repo.formatterCache.miss',
97
				'hit' => 'wikibase.repo.formatterCache.hit',
98
			]
99
		);
100
	}
101
}
102