Completed
Push — master ( 10f95b...bf15d4 )
by Michael
09:02
created

DisplayControllerGet   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96.3%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 106
ccs 26
cts 27
cp 0.963
rs 10

2 Methods

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