Completed
Pull Request — develop (#1328)
by Naveen
03:23
created

Cached_Term_Count::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Vocabulary\Data\Term_Count;
4
/**
5
 * This class is used as decorator around Term_Count interface for
6
 * providing a cache layer.
7
 * @since 3.30.0
8
 * @author Naveen Muthusamy <[email protected]>
9
 */
10
class Cached_Term_Count implements Term_Count {
11
12
	const TRANSIENT_KEY = '_wl_vocabulary_term_count_transient';
13
	/**
14
	 * @var Term_Count
15
	 */
16
	private $term_count;
17
18
19
	/**
20
	 * Cached_Term_Count constructor.
21
	 *
22
	 * @param $term_count Term_Count
23
	 */
24
	public function __construct( $term_count ) {
25
		$this->term_count = $term_count;
26
	}
27
28
	public function get_term_count() {
29
		$data = get_transient( self::TRANSIENT_KEY );
30
31
		if ( ! $data ) {
32
			$data = $this->term_count->get_term_count();
33
			set_transient( self::TRANSIENT_KEY, $data, 8 * 60 * 60 );
34
35
			return $data;
36
		}
37
38
		return $data;
39
	}
40
}
41