Completed
Push — master ( 7665c8...9af7b9 )
by Robbert
61:21 queued 52:46
created

cache::invalidate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 7.608

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 2
cts 10
cp 0.2
crap 7.608
1
<?php
2
	class cache {
3 48
		public function __construct($cache_config) {
4 48
			debug("cache([array])","store");
5
			// init cache store
6 48
			$inst_store = $cache_config["dbms"]."store";
7 48
			include_once($cache_config["code"]."stores/$inst_store.phtml");
8 48
			$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 48
		}
10
11 12
		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 object );
20
			}
21
22
			$data = new object;
23
			$data->filename = $filename;
0 ignored issues
show
Bug introduced by
The property filename does not seem to exist in object.

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 object.

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 object.

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 12
			}
41
42
			$this->cachestore->save("/" . md5($filename) . "/", "pcache", $data, $properties);
43
44
			// $onsave = $this->onTemplateSaved("1944", "ppage", "ppage.view.div1.html.any");
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
		}
46
47 24 View Code Duplication
		public function onTemplateSaved($id, $type, $name) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48 24
			$query = "template.value='$id:$type:$name' order by none";
49 24
			$objects = $this->cachestore->find("/", $query, 0, 0);
50
51
			$template = function($object) {
52
				return $object->data->filename;
53 24
			};
54
55 24
			$result = $this->cachestore->call($template,array(),$objects);
56 24
			$result = array_unique($result);
57
58 24
			foreach ($result as $filename) {
59
				$this->invalidate($filename);
60 18
			}
61 24
		}
62
63 48 View Code Duplication
		public function onObjectSaved($id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64 48
			$query = "objectref.value='$id' order by none";
65 48
			$objects = $this->cachestore->find("/", $query, 0, 0);
66
67 48
			$template = function($object) {
68
				return $object->data->filename;
69 48
			};
70
71 48
			$result = $this->cachestore->call($template,array(),$objects);
72 48
			$result = array_unique($result);
73
74 48
			foreach ($result as $filename) {
75
				$this->invalidate($filename);
76 36
			}
77 48
		}
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