Completed
Push — tar-fix ( d474e8...0c2e2e )
by
unknown
09:21 queued 06:42
created

cache::onObjectSaved()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
ccs 10
cts 12
cp 0.8333
crap 2.0185
1
<?php
2
	class cache {
3 12
		public function __construct($cache_config) {
4 12
			debug("cache([array])","store");
5
			// init cache store
6 12
			$inst_store = $cache_config["dbms"]."store";
7 12
			include_once($cache_config["code"]."stores/$inst_store.phtml");
8 12
			$this->cachestore=new $inst_store($cache_config["root"], $cache_config);
0 ignored issues
show
Bug introduced by
The property cachestore does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
9 12
		}
10
11
		public function save($filename, $objectChain, $templateChain) {
12
			if (!is_array($objectChain)) {
13
				return false;
14
			}
15
			if (!is_array($templateChain)) {
16
				return false;
17
			}
18
			if ( !$this->cachestore->exists('/') ) {
19
				$this->cachestore->save( '/', 'pobject', new baseObject );
20
			}
21
22
			$data = new baseObject;
23
			$data->filename = $filename;
0 ignored issues
show
Bug introduced by
The property filename does not seem to exist in baseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
24
			$data->objectChain = $objectChain;
0 ignored issues
show
Bug introduced by
The property objectChain does not seem to exist in baseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
25
			$data->templateChain = $templateChain;
0 ignored issues
show
Bug introduced by
The property templateChain does not seem to exist in baseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
26
27
                        $properties = array();
28
			$properties["objectref"] = array();
29
			$properties["template"] = array();
30
31
			foreach ($objectChain as $id => $value) {
32
				$properties["objectref"][] = array("name" => "id", "value" => $id);
33
			}
34
			foreach ($templateChain as $id => $template) {
35
				foreach ($template as $name => $value) {
36
					foreach ($value as $type => $dummy) {
37
						$properties["template"][] = array("name" => "name", "value" => $id . ":" . $type . ":" . $name);
38
					}
39
				}
40
			}
41
42
			$this->cachestore->save("/" . md5($filename) . "/", "pcache", $data, $properties);
43
44
			// $onsave = $this->onTemplateSaved("1944", "ppage", "ppage.view.div1.html.any");
45
		}
46
47 6 View Code Duplication
		public function onTemplateSaved($id, $type, $name) {
48 6
			$query = "template.value='$id:$type:$name' order by none";
49 6
			$objects = $this->cachestore->find("/", $query, 0, 0);
50
51
			$template = function($object) {
52
				return $object->data->filename;
53 6
			};
54
55 6
			$result = $this->cachestore->call($template,array(),$objects);
56 6
			$result = array_unique($result);
57
58 6
			foreach ($result as $filename) {
59
				$this->invalidate($filename);
60 6
			}
61 6
		}
62
63 12 View Code Duplication
		public function onObjectSaved($id) {
64 12
			$query = "objectref.value='$id' order by none";
65 12
			$objects = $this->cachestore->find("/", $query, 0, 0);
66
67 12
			$template = function($object) {
68
				return $object->data->filename;
69 12
			};
70
71 12
			$result = $this->cachestore->call($template,array(),$objects);
72 12
			$result = array_unique($result);
73
74 12
			foreach ($result as $filename) {
75
				$this->invalidate($filename);
76 12
			}
77 12
		}
78
79 12
		public function invalidate($filename) {
80 12
			global $store;
81
			$absFilename = $store->get_config("files")."cache/".$filename;
82
			$stamp = time();
83
84
			if (file_exists($absFilename)) {
85
				if (filemtime($absFilename) > $stamp + 2) {  // do not touch file which will expire soon
86
					touch($absFilename, $stamp + 1); // set mtime to now; this means the cache image is now invalid;
87
				}
88
			}
89
		}
90
91
		public function delete($filename) {
92
			$this->cachestore->delete("/" . md5($filename) . "/");
93
		}
94
	}
95
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
96