Completed
Branch master (48c558)
by
unknown
05:41
created

PageCache::renderTemplate()   C

↳ Parent: PageCache

Complexity

Conditions 7
Paths 8

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 48
Code Lines 20

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 8
nop 0
1
<?php
2
namespace Redaxscript\Modules\PageCache;
3
4
use Redaxscript\Cache;
5
use Redaxscript\Template;
6
7
/**
8
 * simple page cache
9
 *
10
 * @since 3.0.0
11
 *
12
 * @package Redaxscript
13
 * @category Modules
14
 * @author Henry Ruhs
15
 */
16
17
class PageCache extends Config
18
{
19
	/**
20
	 * array of the module
21
	 *
22
	 * @var array
23
	 */
24
25
	protected static $_moduleArray =
26
	[
27
		'name' => 'Page cache',
28
		'alias' => 'PageCache',
29
		'author' => 'Redaxmedia',
30
		'description' => 'Simple page cache',
31
		'version' => '3.0.0'
32
	];
33
34
	/**
35
	 * adminPanelNotification
36
	 *
37
	 * @since 3.0.0
38
	 *
39
	 * @return array
40
	 */
41
42
	public function adminPanelNotification()
43
	{
44
		return $this->getNotification();
45
	}
46
47
	/**
48
	 * renderTemplate
49
	 *
50
	 * @since 3.0.0
51
	 *
52
	 * @return mixed
53
	 */
54
55
	public function renderTemplate()
56
	{
57
		/* handle notification */
58
59
		if (!is_dir($this->_configArray['directory']) && !mkdir($this->_configArray['directory']))
60
		{
61
			$this->setNotification('error', $this->_language->get('directory_not_found') . $this->_language->get('colon') . ' ' . $this->_configArray['directory'] . $this->_language->get('point'));
62
		}
63
64
		/* prevent as needed */
65
66
		if ($this->_request->getPost() || $this->_registry->get('noCache'))
67
		{
68
			return false;
69
		}
70
71
		/* cache as needed */
72
73
		$cache = new Cache();
74
		$cache->init($this->_configArray['directory'], $this->_configArray['extension']);
75
		$bundle = $this->_registry->get('fullRoute') . '/' . $this->_registry->get('template') . '/' . $this->_registry->get('language');
76
77
		/* load from cache */
78
79
		if ($cache->validate($bundle, $this->_configArray['lifetime']))
80
		{
81
			$raw = $cache->retrieve($bundle);
82
			$content = preg_replace('/' . $this->_configArray['tokenPlaceholder'] . '/', $this->_registry->get('token'), $this->_uncompress($raw));
0 ignored issues
show
Security Bug introduced by redaxmedia
It seems like $raw defined by $cache->retrieve($bundle) on line 81 can also be of type false; however, Redaxscript\Modules\Page...ageCache::_uncompress() does only seem to accept string|null, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
83
			return
84
			[
85
				'header' => function_exists('gzdeflate') ? 'content-encoding: deflate' : null,
86
				'content' => $this->_compress($content)
87
			];
88
		}
89
90
		/* else store to cache */
91
92
		else
93
		{
94
			$raw = Template\Tag::partial('templates/' . $this->_registry->get('template') . '/index.phtml');
95
			$content = preg_replace('/' . $this->_registry->get('token') . '/', $this->_configArray['tokenPlaceholder'], $raw);
96
			$cache->store($bundle, $this->_compress($content));
97
			return
98
			[
99
				'content' => $raw
100
			];
101
		}
102
	}
103
104
	/**
105
	 * compress
106
	 *
107
	 * @since 3.0.0
108
	 *
109
	 * @param string $content
110
	 *
111
	 * @return string
112
	 */
113
114
	public function _compress($content = null)
115
	{
116
		return function_exists('gzdeflate') ? gzdeflate($content) : $content;
117
	}
118
119
	/**
120
	 * uncompress
121
	 *
122
	 * @since 3.0.0
123
	 *
124
	 * @param string $content
125
	 *
126
	 * @return string
127
	 */
128
129
	public function _uncompress($content = null)
130
	{
131
		return function_exists('gzinflate') ? gzinflate($content) : $content;
132
	}
133
}
134