Passed
Push — master ( 52e05d...6f0859 )
by
unknown
08:31 queued 03:15
created

JSONRequest::execute()   C

Complexity

Conditions 12
Paths 86

Size

Total Lines 111
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 12
eloc 56
c 2
b 1
f 0
nc 86
nop 1
dl 0
loc 111
rs 6.5333

How to fix   Long Method    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
 * JSON Request handler.
5
 *
6
 * This class handles all incoming JSON requests from the client. In short, it receives the JSON,
7
 * decodes JSON data, then sends the requests to the correct modules, and builds the reply JSON. The reply
8
 * JSON is encoded and then returned for reply.
9
 */
10
class JSONRequest {
11
	public function __construct() {}
12
13
	/**
14
	 * Execute incoming JSON request.
15
	 *
16
	 * This function executes the actions in the JSON, which are received from
17
	 * the client. The entire JSON request is processed at once here, and this function
18
	 * is therefore called only once for each HTTP request to the server.
19
	 *
20
	 * @param string $json the json string which is received by the client
21
	 *
22
	 * @return string the built json which will be sent back to the client
23
	 *
24
	 * @todo Reduce overhead by outputting created JSON by outputting directly to php://output instead of returning a
25
	 *       (possibly huge) string containing the serialized JSON
26
	 */
27
	public function execute($json) {
28
		/* If json_encode produces an empty string, wa-js shows an absolutely
29
		 * worthless "Invalid data received from the server" dialog. */
30
		$jsonflags = JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE;
31
32
		try {
33
			// decode JSON data
34
			$data = json_decode_data($json, true);
35
36
			// Reset the bus
37
			$GLOBALS["bus"]->reset();
38
39
			// notify modules that wants to do something at the start of a request
40
			$GLOBALS["bus"]->notify(REQUEST_ENTRYID, REQUEST_START);
41
42
			// Check if the JSON is parsed correctly into an array
43
			$data = $data["zarafa"] ?: false;
44
45
			// @TODO throw exception if zarafa tag is not present
46
			if (is_array($data)) {
47
				// iterate over all module names
48
				foreach ($data as $moduleName => $modules) {
49
					// each module can contain multiple requests using different module ids
50
					foreach ($modules as $moduleId => $moduleData) {
51
						// Create the module via the Dispatcher
52
						$moduleObj = $GLOBALS["dispatcher"]->loadModule($moduleName, $moduleId, $moduleData);
53
54
						// Check if the module is loaded
55
						if (is_object($moduleObj)) {
56
							$moduleObj->loadSessionData();
57
58
							// Execute the actions in the module
59
							$moduleObj->execute();
60
61
							$moduleObj->saveSessionData();
62
						}
63
					}
64
				}
65
			}
66
67
			// notify modules that wants to do something at the end of a request
68
			$GLOBALS["bus"]->notify(REQUEST_ENTRYID, REQUEST_END);
69
70
			// Build the JSON and return it
71
			return json_encode(["zarafa" => $GLOBALS["bus"]->getData()], $jsonflags);
72
		}
73
		catch (ZarafaException $e) {
74
			if (!$e->isHandled) {
75
				$data = [
76
					"error" => [
77
						"type" => ERROR_ZARAFA,
78
						"info" => [
79
							"file" => $e->getFileLine(),
80
							"display_message" => $e->getDisplayMessage(),
81
							"original_message" => $e->getMessage(),
82
						],
83
					],
84
				];
85
86
				return json_encode(["zarafa" => $data], $jsonflags);
87
			}
88
		}
89
		catch (ZarafaErrorException $e) {
90
			if (!$e->isHandled) {
91
				$data = [
92
					"error" => [
93
						"type" => ERROR_GENERAL,
94
						"info" => [
95
							"file" => $e->getFileLine(),
96
							"display_message" => $e->getDisplayMessage(),
97
							"original_message" => $e->getMessage(),
98
						],
99
					],
100
				];
101
102
				return json_encode(["zarafa" => $data], $jsonflags);
103
			}
104
		}
105
		catch (Exception $e) {
106
			// handle exceptions that are not handled by modules
107
			dump($e);
108
109
			$data = [
110
				"error" => [
111
					"type" => ERROR_GENERAL,
112
					"info" => [
113
						"file" => basename($e->getFile()) . ':' . $e->getLine(),
114
						"display_message" => _('An unexpected error has occurred'),
115
						"original_message" => $e->getMessage(),
116
					],
117
				],
118
			];
119
120
			return json_encode(["zarafa" => $data], $jsonflags);
121
		}
122
		catch (Throwable $e) {
123
			// Catch PHP Errors/TypeErrors as JSON to avoid HTTP 500s
124
			dump($e);
125
126
			$data = [
127
				"error" => [
128
					"type" => ERROR_GENERAL,
129
					"info" => [
130
						"file" => basename($e->getFile()) . ':' . $e->getLine(),
131
						"display_message" => _('An unexpected error has occurred'),
132
						"original_message" => $e->getMessage(),
133
					],
134
				],
135
			];
136
137
			return json_encode(["zarafa" => $data], $jsonflags);
138
		}
139
	}
140
}
141