pageCache   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 68
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C call() 0 65 8
1
<?php
2
class pageCache extends \Slim\Middleware
3
{
4
	public function call()
5
	{
6
		global $theme;
7
		if(User::isLoggedIn())
8
		{
9
			$this->next->call();
10
			return;
11
		}
12
13
		$pageURL = $this->app->request()->getResourceUri();
14
		$md5 = md5($pageURL . $theme);
15
		$fileCache = new FileCache();
16
17
		// Pages to cache
18
		$pages = array(
19
			"/kill/" => 60*60*24, // Kill detail pages can easily be cached for many hours since they rarely change..
20
			"/system/" => 60*60, // 1 hour cache on the system page
21
			"/region/" => 60*60, // 1 hour cache on the region page
22
			"/group/" => 60*60, // 1 hour cache on the groups view
23
			"/ship/" => 60*15, // 15 minutes on ship view
24
			"/character/" => 60*5, // 5 minutes on character view
25
			"/corporation/" => 60*5, // 5 minutes on corporation view
26
			"/alliance/" => 60*5, // 5 minutes on alliance view
27
			"/faction/" => 60*15, // 15 minutes on faction view
28
			"/item/" => 60*60*24, // 1 Day on item view, shit barely changes..
29
			"/information/" => 60*5, // 5 minutes on information, tho it's probably not needed tbh
30
		);
31
32
		// Default cachetime is 0seconds
33
		$cacheTime = 0;
34
		foreach($pages as $page => $cTime)
35
		{
36
			if(stristr($pageURL, $page)) // Only do shit on the kill page for now... Shou
37
			{
38
				$cacheTime = $cTime;
39
			}
40
		}
41
42
		// Default headers
43
		header("X-PageCache: false");
44
		header("X-PageCache-Time: {$cacheTime}");
45
46
		// Application response
47
		$rsp = $this->app->response();
48
49
		// Get the cached result if it exists
50
		$data = $fileCache->get($md5);
51
		if (isset($data) && !empty($data["body"]))
52
		{
53
			// cache hit... return the cached content
54
			header("X-PageCache: true");
55
			$rsp["Content-Type"] = $data["content_type"];
56
			$rsp->body($data["body"]);
0 ignored issues
show
Bug introduced by
The method body cannot be called on $rsp (of type array<string,?,{"Content-Type":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
57
			return;
58
		}
59
60
		// cache miss... continue on to generate the page
61
		$this->next->call();
62
63
		// cache result for future look up
64
		if ($rsp->status() == 200) {
65
			if($cacheTime > 0)
66
				$fileCache->set($md5, array("key" => $pageURL, "content_type" => $rsp["Content-Type"], "body" => $rsp->body()), $cacheTime);
67
		}
68
	}
69
}
70