Completed
Pull Request — master (#126)
by Deven
03:40
created

IncidentsController::create()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 25
ccs 20
cts 20
cp 1
rs 8.8571
cc 3
eloc 19
nc 2
nop 0
crap 3
1
<?php
2
/* vim: set noexpandtab sw=2 ts=2 sts=2: */
3
namespace App\Controller;
4
5
use App\Controller\AppController;
6
use Cake\Network\Exception\NotFoundException;
7
use App\Utility\Sanitize;
8
/**
9
 * Incidents controller handling incident creation and rendering
10
 *
11
 * phpMyAdmin Error reporting server
12
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
13
 *
14
 * Licensed under The MIT License
15
 * For full copyright and license information, please see the LICENSE.txt
16
 * Redistributions of files must retain the above copyright notice.
17
 *
18
 * @copyright     Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
19
 * @package       Server.Controller
20
 * @link          https://www.phpmyadmin.net/
21
 * @license       https://opensource.org/licenses/mit-license.php MIT License
22
 */
23
24
/**
25
 * Incidents controller handling incident creation and rendering
26
 *
27
 * @package       Server.Controller
28
 */
29
class IncidentsController extends AppController {
30
	public $uses = array("Incident", "Notification");
31
32 1
	public function create() {
33 1
		$bugReport = $this->request->input('json_decode', true);
34 1
		$result = $this->Incidents->createIncidentFromBugReport($bugReport);
35 1
		if (count($result) > 0 
36 1
			&& !in_array(false, $result)
37 1
		) {
38
			$response = array(
39 1
				"success" => true,
40 1
				"message" => "Thank you for your submission",
41 1
				"incident_id" => $result,		// Return a list of incident ids.
42 1
			);
43 1
		} else {
44
			$response = array(
45 1
				"success" => false,
46 1
				"message" => "There was a problem with your submission.",
47 1
			);
48
		}
49 1
		$this->autoRender = false;
50 1
		$this->response->header([
51 1
			'Content-Type' => 'application/json',
52
			'X-Content-Type-Options' => 'nosniff'
53 1
		]);
54 1
        $this->response->body(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
55 1
        return $this->response;
56
	}
57
58 1
	public function json($id) {
59 1
		if (!$id) {
60
			throw new NotFoundException(__('Invalid Incident'));
61
		}
62
63 1
		$this->Incidents->recursive = -1;
64 1
		$incident = $this->Incidents->findById($id)->all()->first();
65 1
		if (!$incident) {
66
			throw new NotFoundException(__('Invalid Incident'));
67
		}
68
69 1
		$incident['full_report'] =
70 1
				json_decode($incident['full_report'], true);
71 1
		$incident['stacktrace'] =
72 1
				json_decode($incident['stacktrace'], true);
73
74 1
		$this->autoRender = false;
75 1
        $this->response->body(json_encode($incident, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
76 1
        return $this->response;
77
	}
78
79 1
	public function view($incidentId) {
80 1
		if (!$incidentId) {
81
			throw new NotFoundException(__('Invalid Incident'));
82
		}
83
84 1
		$incident = $this->Incidents->findById($incidentId)->all()->first();
85 1
		if (!$incident) {
86
			throw new NotFoundException(__('Invalid Incident'));
87
		}
88
89 1
		$incident['full_report'] =
90 1
				json_decode($incident['full_report'], true);
91 1
		$incident['stacktrace'] =
92 1
				json_decode($incident['stacktrace'], true);
93
94 1
		$this->set('incident', $incident);
95 1
	}
96
}
97