Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

MslsSqlCacher::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsSqlCacher
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 1.0
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Wrapper to avoid direct SQL without caching
12
 * @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/91e5fe9ada922a82a32b83eaabad1e2a2ee50338/MslsSqlCacher.php
13
 * @method mixed get_var( string $sql )
14
 * @method array get_results( string $sql )
15
 * @method string prepare( string $sql, mixed $a, $b = '', $c = '' )
16
 * @method mixed query( string $sql )
17
 * @property string $posts
18
 * @property string $options
19
 * @property string $blogs
20
 * @property int $blogid
21
 * @property int $siteid
22
 * @package Msls
23
 */
24
class MslsSqlCacher {
25
26
	/**
27
	 * Database object
28
	 *
29
	 * @var object $db
30
	 */
31
	protected $db;
32
33
	/**
34
	 * Name of the object which created this object
35
	 *
36
	 * @var string $caller
37
	 */
38
	protected $caller;
39
40
	/**
41
	 * Parameters are used to create the key for the cached resultset
42
	 *
43
	 * @var mixed $params
44
	 */
45
	protected $params;
46
47
	/**
48
	 * @param \WPDB $db
49
	 * @param $caller
50
	 */
51
	public function __construct( \WPDB $db, $caller ) {
52
		$this->db     = $db;
53
		$this->caller = $caller;
54
	}
55
56
	/**
57
	 * Factory
58
	 *
59
	 * @uses \WPDB $wpdb
60
	 *
61
	 * @param string $caller
62
	 *
63
	 * @return MslsSqlCacher
64
	 */
65
	public static function init( $caller ) {
66
		global $wpdb;
67
68
		return new self( $wpdb, $caller );
69
	}
70
71
	/**
72
	 * Set params
73
	 *
74
	 * @param mixed $params
75
	 *
76
	 * @return MslsSqlCacher
77
	 */
78
	public function set_params( $params ) {
79
		$this->params = $params;
80
81
		return $this;
82
	}
83
84
	/**
85
	 * Get the name of the key which is in use for the cached object
86
	 *
87
	 * @return string
88
	 */
89
	public function get_key() {
90
		$params = (
91
		is_array( $this->params ) ?
92
			implode( '_', $this->params ) :
93
			$this->params
94
		);
95
96
		return $this->caller . '_' . $params;
97
	}
98
99
	/**
100
	 * Magic __get
101
	 *
102
	 * @param string $key
103
	 *
104
	 * @return mixed
105
	 */
106
	public function __get( $key ) {
107
		return ( isset( $this->db->$key ) ? $this->db->$key : null );
108
	}
109
110
	/**
111
	 * Call a method of the db-object with the needed args and cache the result
112
	 *
113
	 * @param string $method
114
	 * @param array $args
115
	 *
116
	 * @return mixed
117
	 */
118
	public function __call( $method, $args ) {
119
		if ( 'get_' != substr( $method, 0, 4 ) ) {
120
			$result = call_user_func_array( [ $this->db, $method ], $args );
121
		} else {
122
			$key    = $this->get_key();
123
			$result = wp_cache_get( $key );
124
			if ( false === $result ) {
125
				$result = call_user_func_array( [ $this->db, $method ], $args );
126
				wp_cache_set( $key, $result );
127
			}
128
		}
129
130
		return $result;
131
	}
132
133
}
134