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

WebApplication::setErrorHeader()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 5
nop 1
crap 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;
10
11
use Joomla\Application\AbstractWebApplication;
12
use Ramsey\Uuid\Uuid;
13
use TheIconic\Tracking\GoogleAnalytics\Analytics;
14
15
/**
16
 * Web application for the stats server
17
 *
18
 * @since  1.0
19
 */
20
class WebApplication extends AbstractWebApplication
21
{
22
	/**
23
	 * Application analytics object.
24
	 *
25
	 * @var    Analytics
26
	 * @since  1.0
27
	 */
28
	private $analytics;
29
30
	/**
31
	 * Response mime type.
32
	 *
33
	 * @var    string
34
	 * @since  1.0
35
	 */
36
	public $mimeType = 'application/json';
37
38
	/**
39
	 * Application router.
40
	 *
41
	 * @var    Router
42
	 * @since  1.0
43
	 */
44
	private $router;
45
46
	/**
47
	 * Method to run the application routines.
48
	 *
49
	 * @return  void
50
	 *
51
	 * @since   1.0
52
	 */
53 5
	protected function doExecute()
54
	{
55
		// On a GET request to the live domain, submit analytics data
56 5
		if ($this->input->getMethod() === 'GET'
57 5
			&& strpos($this->input->server->getString('HTTP_HOST', ''), 'developer.joomla.org') === 0
58 5
			&& $this->analytics)
59
		{
60
			$this->analytics->setAsyncRequest(true)
61
				->setProtocolVersion('1')
62
				->setTrackingId('UA-544070-16')
63
				->setClientId(Uuid::uuid4()->toString())
64
				->setDocumentPath($this->get('uri.base.path'))
65
				->setIpOverride($this->input->server->getString('REMOTE_ADDR', '127.0.0.1'))
66
				->setUserAgentOverride($this->input->server->getString('HTTP_USER_AGENT', 'JoomlaStats/1.0'));
67
68
			// Don't allow sending Analytics data to cause a failure
69
			try
70
			{
71
				$this->analytics->sendPageview();
72
			}
73
			catch (\Exception $e)
74
			{
75
				// Log the error for reference
76
				$this->getLogger()->error(
77
					'Error sending analytics data.',
78
					['exception' => $e]
79
				);
80
			}
81
		}
82
83
		try
84
		{
85 5
			$this->router->getController($this->get('uri.route'))->execute();
86
		}
87 4
		catch (\Throwable $e)
88
		{
89
			// Log the error for reference
90 4
			$this->getLogger()->error(
91 4
				sprintf('Uncaught Throwable of type %s caught.', get_class($e)),
92 4
				['exception' => $e]
93
			);
94
95 4
			$this->setErrorHeader($e);
96
97
			$data = [
98 4
				'error'   => true,
99 4
				'message' => $e->getMessage(),
100
			];
101
102 4
			$this->setBody(json_encode($data));
103
		}
104 5
	}
105
106
	/**
107
	 * Set the application's analytics object.
108
	 *
109
	 * @param   Analytics  $analytics  Analytics object to set.
110
	 *
111
	 * @return  $this
112
	 *
113
	 * @since   1.0
114
	 */
115
	public function setAnalytics(Analytics $analytics) : WebApplication
116
	{
117
		$this->analytics = $analytics;
118
119
		return $this;
120
	}
121
122
	/**
123
	 * Set the HTTP Response Header for error conditions.
124
	 *
125
	 * @param   \Throwable  $exception  The Throwable object to process.
126
	 *
127
	 * @return  void
128
	 *
129
	 * @since   1.0
130
	 */
131 4
	private function setErrorHeader(\Throwable $exception)
132
	{
133 4
		switch ($exception->getCode())
134
		{
135 4
			case 401:
136 1
				$this->setHeader('HTTP/1.1 401 Unauthorized', 401, true);
137
138 1
				break;
139
140 3
			case 403:
141 1
				$this->setHeader('HTTP/1.1 403 Forbidden', 403, true);
142
143 1
				break;
144
145 2
			case 404:
146 1
				$this->setHeader('HTTP/1.1 404 Not Found', 404, true);
147
148 1
				break;
149
150 1
			case 500:
151
			default:
152 1
				$this->setHeader('HTTP/1.1 500 Internal Server Error', 500, true);
153
154 1
				break;
155
		}
156 4
	}
157
158
	/**
159
	 * Set the application's router.
160
	 *
161
	 * @param   Router  $router  Router object to set.
162
	 *
163
	 * @return  $this
164
	 *
165
	 * @since   1.0
166
	 */
167 1
	public function setRouter(Router $router) : WebApplication
168
	{
169 1
		$this->router = $router;
170
171 1
		return $this;
172
	}
173
}
174