Passed
Push — master ( cb3898...d20083 )
by Fabio
05:58
created

TPageNoCacheBehavior::addNoCacheMeta()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 27
c 1
b 0
f 0
nc 17
nop 2
dl 0
loc 34
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TPageNoCacheBehavior class file.
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Util\Behaviors;
12
13
use Prado\TPropertyValue;
14
use Prado\Web\UI\WebControls\TMetaTag;
15
16
/**
17
 * TPageNoCacheBehavior class.
18
 *
19
 * TPageNoCacheBehavior attaches to pages and adds no-cache meta to the head.
20
 *
21
 * {@link getCheckMetaNoCache} specifies whether or not to check the existing
22
 * meta tags (in THead of the TPage) before adding the no cache tags. By default
23
 * getCheckMetaNoCache is turned off for performance.
24
 *
25
 * @author Brad Anderson <[email protected]>
26
 * @package Prado\Util\Behaviors
27
 * @since 4.2.0
28
 */
29
class TPageNoCacheBehavior extends \Prado\Util\TBehavior
30
{
31
	/** @var bool check the existing meta tags for the no cache before adding them */
32
	private $_checkMetaNoCache = false;
33
	
34
	/**
35
	 * This handles the TPage.OnInitComplete event to place no-cache
36
	 * meta in the head.
37
	 * @return array of events as keys and methods as values
38
	 */
39
	public function events()
40
	{
41
		return ['OnInitComplete' => 'addNoCacheMeta'];
42
	}
43
	
44
	/**
45
	 * This method places no-cache meta in the head.
46
	 * @param $page object raising the event
47
	 * @param $param mixed the parameter of the raised event
48
	 */
49
	public function addNoCacheMeta($page, $param)
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
	public function addNoCacheMeta($page, /** @scrutinizer ignore-unused */ $param)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
	{
51
		if ($head = $page->getHead()) {
52
			$hasExpires = $hasPragma = $hasCacheControl = false;
53
			$metatags = $head->getMetaTags();
54
			if ($this->_checkMetaNoCache) {
55
				foreach ($metatags as $meta) {
56
					$httpEquiv = strtolower($meta->getHttpEquiv());
57
					if ($httpEquiv == 'expires') {
58
						$hasExpires = true;
59
					} elseif ($httpEquiv == 'pragma') {
60
						$hasPragma = true;
61
					} elseif ($httpEquiv == 'cache-control') {
62
						$hasCacheControl = true;
63
					}
64
				}
65
			}
66
			if (!$hasExpires) {
67
				$meta = new TMetaTag();
68
				$meta->setHttpEquiv('Expires');
69
				$meta->setContent('Fri, Jan 01 1900 00:00:00 GMT');
70
				$metatags->add($meta);
71
			}
72
			if (!$hasPragma) {
73
				$meta = new TMetaTag();
74
				$meta->setHttpEquiv('Pragma');
75
				$meta->setContent('no-cache');
76
				$metatags->add($meta);
77
			}
78
			if (!$hasCacheControl) {
79
				$meta = new TMetaTag();
80
				$meta->setHttpEquiv('Cache-Control');
81
				$meta->setContent('no-cache');
82
				$metatags->add($meta);
83
			}
84
		}
85
	}
86
	
87
	/**
88
	 * @return bool checks existing meta tags for no cache
89
	 */
90
	public function getCheckMetaNoCache()
91
	{
92
		return $this->_checkMetaNoCache;
93
	}
94
	
95
	/**
96
	 * @param bool $value checks existing meta tags for no cache
97
	 */
98
	public function setCheckMetaNoCache($value)
99
	{
100
		$this->_checkMetaNoCache = TPropertyValue::ensureBoolean($value);
101
	}
102
}
103