Completed
Push — master ( 82cbc4...aed5b3 )
by Michal
12s
created

IncidentsController::create()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 30
ccs 14
cts 15
cp 0.9333
rs 8.8571
cc 3
eloc 20
nc 2
nop 0
crap 3.0026
1
<?php
2
3
/* vim: set expandtab sw=4 ts=4 sts=4: */
4
/**
5
 * Incidents controller handling incident creation and rendering.
6
 *
7
 * phpMyAdmin Error reporting server
8
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 *
17
 * @see      https://www.phpmyadmin.net/
18
 */
19
20
namespace App\Controller;
21
22
use Cake\Network\Exception\NotFoundException;
23
24
/**
25
 * Incidents controller handling incident creation and rendering.
26
 */
27
class IncidentsController extends AppController
28
{
29
    public $uses = array('Incident', 'Notification');
30
31 1
    public function create()
32
    {
33
        // Only allow POST requests
34 1
        $this->request->allowMethod(['post']);
35
36 1
        $bugReport = $this->request->input('json_decode', true);
37 1
        $result = $this->Incidents->createIncidentFromBugReport($bugReport);
38 1
        if (count($result) > 0
39 1
            && !in_array(false, $result)
40
        ) {
41
            $response = array(
42 1
                'success' => true,
43 1
                'message' => 'Thank you for your submission',
44 1
                'incident_id' => $result,        // Return a list of incident ids.
45
            );
46
        } else {
47
            $response = array(
48
                'success' => false,
49
                'message' => 'There was a problem with your submission.',
50
            );
51
        }
52 1
        $this->autoRender = false;
53 1
        $this->response->header(array(
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\Response::header() has been deprecated with message: 3.4.0 Use `withHeader()`, `getHeaderLine()` and `getHeaders()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54 1
            'Content-Type' => 'application/json',
55
            'X-Content-Type-Options' => 'nosniff',
56
        ));
57 1
        $this->response->body(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\Response::body() has been deprecated with message: 3.4.0 Mutable response methods are deprecated. Use `withBody()` and `getBody()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58
59 1
        return $this->response;
60
    }
61
62 1
    public function json($id)
63
    {
64 1
        if (!$id) {
65
            throw new NotFoundException(__('Invalid Incident'));
66
        }
67
68 1
        $this->Incidents->recursive = -1;
69 1
        $incident = $this->Incidents->findById($id)->all()->first();
70 1
        if (!$incident) {
71
            throw new NotFoundException(__('Invalid Incident'));
72
        }
73
74 1
        $incident['full_report'] =
75 1
            json_decode($incident['full_report'], true);
76 1
        $incident['stacktrace'] =
77 1
            json_decode($incident['stacktrace'], true);
78
79 1
        $this->autoRender = false;
80 1
        $this->response->body(json_encode($incident, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\Response::body() has been deprecated with message: 3.4.0 Mutable response methods are deprecated. Use `withBody()` and `getBody()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
81
82 1
        return $this->response;
83
    }
84
85 1
    public function view($incidentId)
86
    {
87 1
        if (!$incidentId) {
88
            throw new NotFoundException(__('Invalid Incident'));
89
        }
90
91 1
        $incident = $this->Incidents->findById($incidentId)->all()->first();
92 1
        if (!$incident) {
93
            throw new NotFoundException(__('Invalid Incident'));
94
        }
95
96 1
        $incident['full_report'] =
97 1
            json_decode($incident['full_report'], true);
98 1
        $incident['stacktrace'] =
99 1
            json_decode($incident['stacktrace'], true);
100
101 1
        $this->set('incident', $incident);
102 1
    }
103
}
104