CacheableFetch   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B fetch() 0 20 9
A clear() 0 3 1
1
<?php
2
	/**
3
	 * Copyright: Deux Huit Huit 2014
4
	 * LICENCE: MIT https://deuxhuithuit.mit-license.org
5
	 */
6
	
7
	if(!defined("__IN_SYMPHONY__")) die("<h2>Error</h2><p>You cannot directly access this file</p>");
8
	
9
	class CacheableFetch {
10
		
11
		private $className;
12
		private $cache = array();
13
		
14
		/**
15
		 * @param string $className
16
		 */
17
		public function __construct($className) {
18
			$this->className = $className;
19
		}
20
		
21
		public function fetch($id, $secondId = null) {
22
			$args = func_get_args();
23
			if (!$id || $secondId || is_array($id)) {
24
				$id = sha1(serialize($args));
25
			}
26
			if ($id && isset($this->cache[$id])) {
27
				return $this->cache[$id];
28
			}
29
			$ret = forward_static_call_array(array($this->className, 'fetch'), $args);
30
			if ($id) {
31
				if (is_array($ret)) {
32
					foreach ($ret as $key => $value) {
33
						$this->cache[$key] = $value;
34
					}
35
				} else {
36
					$this->cache[$id] = $ret;
37
				}
38
			}
39
			return $ret;
40
		}
41
		
42
		public function clear() {
43
			$this->cache = array();
44
		}
45
	}