Completed
Push — master ( 3dda54...befd6a )
by Michael
02:11
created

Application   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75.76%

Importance

Changes 11
Bugs 0 Features 1
Metric Value
wmc 8
c 11
b 0
f 1
lcom 1
cbo 4
dl 0
loc 102
ccs 25
cts 33
cp 0.7576
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B doExecute() 0 24 2
B setErrorHeader() 0 26 5
A setRouter() 0 6 1
1
<?php
2
3
namespace Stats;
4
5
use Joomla\Application\AbstractWebApplication;
6
7
/**
8
 * Web application for the stats server
9
 *
10
 * @since  1.0
11
 */
12
class Application extends AbstractWebApplication
13
{
14
	/**
15
	 * Response mime type.
16
	 *
17
	 * @var    string
18
	 * @since  1.0
19
	 */
20
	public $mimeType = 'application/json';
21
22
	/**
23
	 * Application router.
24
	 *
25
	 * @var    Router
26
	 * @since  1.0
27
	 */
28
	private $router;
29
30
	/**
31
	 * Method to run the application routines.
32
	 *
33
	 * @return  void
34
	 *
35
	 * @since   1.0
36
	 */
37 2
	public function doExecute()
38
	{
39
		try
40
		{
41 2
			$this->router->getController($this->get('uri.route'))->execute();
42
		}
43 2
		catch (\Exception $e)
44
		{
45
			// Log the error for reference
46 1
			$this->getLogger()->error(
47 1
				sprintf('Uncaught Exception of type %s caught.', get_class($e)),
48 1
				['exception' => $e]
49 1
			);
50
51 1
			$this->setErrorHeader($e);
52
53
			$data = [
54 1
				'error'   => true,
55 1
				'message' => $e->getMessage(),
56 1
			];
57
58 1
			$this->setBody(json_encode($data));
59
		}
60 2
	}
61
62
	/**
63
	 * Set the HTTP Response Header for error conditions.
64
	 *
65
	 * @param   \Exception  $exception  The Exception object to process.
66
	 *
67
	 * @return  void
68
	 *
69
	 * @since   1.0
70
	 */
71 1
	private function setErrorHeader(\Exception $exception)
72
	{
73 1
		switch ($exception->getCode())
74
		{
75 1
			case 401:
76
				$this->setHeader('HTTP/1.1 401 Unauthorized', 401, true);
77
78
				break;
79
80 1
			case 403:
81
				$this->setHeader('HTTP/1.1 403 Forbidden', 403, true);
82
83
				break;
84
85 1
			case 404:
86 1
				$this->setHeader('HTTP/1.1 404 Not Found', 404, true);
87
88 1
				break;
89
90
			case 500:
91
			default:
92
				$this->setHeader('HTTP/1.1 500 Internal Server Error', 500, true);
93
94
				break;
95 1
		}
96 1
	}
97
98
	/**
99
	 * Set the application's router.
100
	 *
101
	 * @param   Router  $router  Router object to set.
102
	 *
103
	 * @return  $this
104
	 *
105
	 * @since   1.0
106
	 */
107 1
	public function setRouter(Router $router)
108
	{
109 1
		$this->router = $router;
110
111 1
		return $this;
112
	}
113
}
114