Completed
Push — master ( 8961be...e7d2ec )
by Michael
05:37
created

DisplayControllerGet   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 93.94%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 106
ccs 31
cts 33
cp 0.9394
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B execute() 0 48 5
A cacheData() 0 7 1
1
<?php
2
3
namespace Stats\Controllers;
4
5
use Joomla\Controller\AbstractController;
6
use Joomla\Cache\Item\Item;
7
use Psr\Cache\CacheItemPoolInterface;
8
use Stats\Views\Stats\StatsJsonView;
9
10
/**
11
 * Controller for displaying submitted statistics data.
12
 *
13
 * @method         \Stats\WebApplication  getApplication()  Get the application object.
14
 * @property-read  \Stats\WebApplication  $app              Application object
15
 *
16
 * @since          1.0
17
 */
18
class DisplayControllerGet extends AbstractController
19
{
20
	/**
21
	 * The cache item pool.
22
	 *
23
	 * @var    CacheItemPoolInterface
24
	 * @since  1.0
25
	 */
26
	private $cache;
27
28
	/**
29
	 * JSON view for displaying the statistics.
30
	 *
31
	 * @var    StatsJsonView
32
	 * @since  1.0
33
	 */
34
	private $view;
35
36
	/**
37
	 * Constructor.
38
	 *
39
	 * @param   StatsJsonView           $view   JSON view for displaying the statistics.
40
	 * @param   CacheItemPoolInterface  $cache  The cache item pool.
41
	 *
42
	 * @since   1.0
43
	 */
44 1
	public function __construct(StatsJsonView $view, CacheItemPoolInterface $cache)
45
	{
46 1
		$this->cache = $cache;
47 1
		$this->view  = $view;
48 1
	}
49
50
	/**
51
	 * Execute the controller.
52
	 *
53
	 * @return  boolean
54
	 *
55
	 * @since   1.0
56
	 */
57 2
	public function execute()
58
	{
59
		// Check if we are allowed to receive the raw data
60 2
		$authorizedRaw = $this->getInput()->server->getString('HTTP_JOOMLA_RAW', 'fail') === $this->getApplication()->get('stats.rawdata', false);
61
62
		// Check if a single data source is requested
63 2
		$source = $this->getInput()->getString('source');
64
65 2
		$this->view->isAuthorizedRaw($authorizedRaw);
66 2
		$this->view->setSource($source);
67
68
		// Serve cached data if the cache layer is enabled and the raw data source is not requested
69 2
		if ($this->getApplication()->get('cache.enabled', false) && !$authorizedRaw)
70 2
		{
71 1
			$key = md5(get_class($this->view) . __METHOD__ . $source);
72
73 1
			if ($this->cache->hasItem($key))
74 1
			{
75 1
				$item = $this->cache->getItem($key);
76
77
				// Make sure we got a hit on the item, otherwise we'll have to re-cache
78 1
				if ($item->isHit())
79 1
				{
80 1
					$body = $item->get();
81 1
				}
82
				else
83
				{
84
					$body = $this->view->render();
85
86
					$this->cacheData($key, $body);
87
				}
88 1
			}
89
			else
90
			{
91 1
				$body = $this->view->render();
92
93 1
				$this->cacheData($key, $body);
94
			}
95 1
		}
96
		else
97
		{
98 1
			$body = $this->view->render();
99
		}
100
101 2
		$this->getApplication()->setBody($body);
102
103 2
		return true;
104
	}
105
106
	/**
107
	 * Store the given data to the cache pool.
108
	 *
109
	 * @param   string  $key   The key for the cache item.
110
	 * @param   mixed   $data  The data to be stored to cache.
111
	 *
112
	 * @return  void
113
	 *
114
	 * @since   1.0
115
	 */
116 1
	private function cacheData($key, $data)
117
	{
118 1
		$item = (new Item($key, $this->getApplication()->get('cache.lifetime', 900)))
119 1
			->set($data);
120
121 1
		$this->cache->save($item);
122 1
	}
123
}
124