Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Moved creation logic from report to incident
- Moved the submit method from ReportsController to create in IncidentController
- Created a method with its helpers to handle incident creation
- added validations
  • Loading branch information
m0hamed committed Aug 11, 2013
1 parent 656d825 commit 874a63e
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 13 deletions.
27 changes: 27 additions & 0 deletions app/Controller/IncidentsController.php
@@ -0,0 +1,27 @@
<?php
/* vim: set noexpandtab sw=2 ts=2 sts=2: */

App::uses('Sanitize', 'Utility');
App::uses('AppController', 'Controller');

class IncidentsController extends AppController {

public function create() {
$bugReport = $this->request->input('json_decode', true);
if ($this->Incident->createIncidentFromBugReport($bugReport)) {
$response = array(
"success" => true,
"message" => "Thank you for your submission",
"report_id" => $this->Incident->id,
);
} else {
$response = array(
"success" => false,
"message" => "There was a problem with your submission."
);
}
$this->autoRender = false;
return json_encode($response);
}

}
13 changes: 0 additions & 13 deletions app/Controller/ReportsController.php
Expand Up @@ -72,19 +72,6 @@ public function json($id) {
return json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}

public function submit() {
$report = $this->request->input('json_decode', true);
$this->Report->create(array('status' => 'new'));
$this->Report->saveFromSubmission($report);
$response = array(
"success" => true,
"message" => "Thank you for your submission",
"report_id" => $this->Report->id,
);
$this->autoRender = false;
return json_encode($response);
}

public function data_tables() {
$aColumns = array('id', 'error_name', 'error_message', 'pma_version',
'status');
Expand Down
103 changes: 103 additions & 0 deletions app/Model/Incident.php
@@ -1,6 +1,109 @@
<?php
/* vim: set noexpandtab sw=2 ts=2 sts=2: */

App::uses('AppModel', 'Model');

class Incident extends AppModel {

public $validate = array(
'error_message' => array(
'rule' => 'notEmpty',
'required' => true,
),
'pma_version' => array(
'rule' => 'notEmpty',
'required' => true,
),
'php_version' => array(
'rule' => 'notEmpty',
'required' => true,
),
'full_report' => array(
'rule' => 'notEmpty',
'required' => true,
),
'stacktrace' => array(
'rule' => 'notEmpty',
'required' => true,
),
'browser' => array(
'rule' => 'notEmpty',
'required' => true,
),
);

public $belongsTo = array('Report');

public function getClosestReport($exception) {
List($location, $linenumber) = $this->_getProperLevel($exception["stack"]);
$report = $this->Report->findByLocationAndLinenumber($location, $linenumber);
return $report;
}

public function createIncidentFromBugReport($bugReport) {
$schematizedIncident = $this->_getSchematizedIncident($bugReport);
$closestReport = $this->getClosestReport($bugReport["exception"]);

if($closestReport) {
$schematizedIncident["report_id"] = $closestReport["Report"]["id"];
return $this->save($schematizedIncident);
} else {
$report = $this->getReportDetails($bugReport);
$data = array(
'Incident' => $schematizedIncident;
'Report' => $report;
);
$this->saveAssociated($data);
}
}

protected function _getReportDetails($bugReport) {
List($location, $linenumber) =
$this->_getProperLevel($bugReport["exception"]["stack"]);

$reportDetails = array(
'error_message' => 'error_message',
'error_name' => 'error_name',
'status' => 'new',
'location' => $location,
'linenumber' => $linenumber,
);
return $reportDetails;
}

protected function _getSchematizedIncident($bugReport) {
$schematizedReport = array(
'pma_version' => $bugReport['pma_version'],
'php_version' => $this->getSimplePhpVersion($bugReport['php_version']),
'steps' => $bugReport['steps'],
'error_message' => $bugReport['exception']['message'],
'error_name' => $bugReport['exception']['name'],
'browser' => $bugReport['browser_name'] . " "
. $this->getMajorVersion($bugReport['browser_version']),
'user_os' => $bugReport['user_os'],
'server_software' => $this->getServer($bugReport['server_software']),
'full_report' => json_encode($bugReport),
'stacktrace' => json_encode($bugReport['exception']['stack']),
);

return $schematizedReport;
}

protected function _getIdentifyingLocation($stacktrace) {
foreach ($stacktrace as $level) {
if (isset($level["filename"])) {
if ($level["filename"] !== "tracekit.js"
&& $level["filename"] !== "error_report.js") {
return array($level["filename"], $level["line"]);
} else {
continue;
}
}
if (isset($level["uri"])) {
return array($level["uri"], $level["line"]);
} else {
return array($level["url"], $level["line"]);
}
}
}
}

0 comments on commit 874a63e

Please sign in to comment.