Passed
Push — master ( 1aba9a...2acbdd )
by William
04:40
created

IncidentsController::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 22
c 2
b 0
f 1
nc 4
nop 0
dl 0
loc 38
ccs 0
cts 31
cp 0
crap 20
rs 9.568
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\Core\Configure;
23
use Cake\Http\Exception\NotFoundException;
24
use Cake\ORM\TableRegistry;
25
26
/**
27
 * Incidents controller handling incident creation and rendering.
28
 */
29
class IncidentsController extends AppController
30
{
31
    public $uses = [
32
        'Incident',
33
        'Notification',
34
    ];
35
36
    public $components = ['Mailer'];
37
38
    public function create()
39
    {
40
        // Only allow POST requests
41
        $this->request->allowMethod(['post']);
42
43
        $bugReport = $this->request->input('json_decode', true);
44
        $result = $this->Incidents->createIncidentFromBugReport($bugReport);
0 ignored issues
show
Bug Best Practice introduced by
The property Incidents does not exist on App\Controller\IncidentsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
45
46
        if (count($result['incidents']) > 0
47
            && ! in_array(false, $result['incidents'])
48
        ) {
49
            $response = [
50
                'success' => true,
51
                'message' => 'Thank you for your submission',
52
                'incident_id' => $result['incidents'],        // Return a list of incident ids.
53
            ];
54
        } else {
55
            $response = [
56
                'success' => false,
57
                'message' => 'There was a problem with your submission.',
58
            ];
59
        }
60
        $this->autoRender = false;
61
        $this->response->header([
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::header() has been deprecated: 3.4.0 Use `withHeader()`, `getHeaderLine()` and `getHeaders()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
        /** @scrutinizer ignore-deprecated */ $this->response->header([

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

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

Loading history...
62
            'Content-Type' => 'application/json',
63
            'X-Content-Type-Options' => 'nosniff',
64
        ]);
65
        $this->response->body(
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::body() has been deprecated: 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

65
        /** @scrutinizer ignore-deprecated */ $this->response->body(

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

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

Loading history...
66
            json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
67
        );
68
69
        // For all the newly added reports,
70
        // send notification emails
71
        foreach ($result['reports'] as $report_id) {
72
            $this->_sendNotificationMail($report_id);
73
        }
74
75
        return $this->response;
76
    }
77
78
    public function json($id)
79
    {
80
        if (! isset($id) || ! $id) {
81
            throw new NotFoundException(__('Invalid Incident'));
82
        }
83
84
        $this->Incidents->recursive = -1;
0 ignored issues
show
Bug Best Practice introduced by
The property Incidents does not exist on App\Controller\IncidentsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
85
        $incident = $this->Incidents->findById($id)->all()->first();
86
        if (! $incident) {
87
            throw new NotFoundException(__('Invalid Incident'));
88
        }
89
90
        $incident['full_report'] =
91
            json_decode($incident['full_report'], true);
92
        $incident['stacktrace'] =
93
            json_decode($incident['stacktrace'], true);
94
95
        $this->autoRender = false;
96
        $this->response->body(json_encode($incident, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::body() has been deprecated: 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

96
        /** @scrutinizer ignore-deprecated */ $this->response->body(json_encode($incident, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

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

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

Loading history...
97
98
        return $this->response;
99
    }
100
101
    public function view($incidentId)
102
    {
103
        if (! isset($incidentId) || ! $incidentId) {
104
            throw new NotFoundException(__('Invalid Incident'));
105
        }
106
107
        $incident = $this->Incidents->findById($incidentId)->all()->first();
0 ignored issues
show
Bug Best Practice introduced by
The property Incidents does not exist on App\Controller\IncidentsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
108
        if (! $incident) {
109
            throw new NotFoundException(__('Invalid Incident'));
110
        }
111
112
        $incident['full_report'] =
113
            json_decode($incident['full_report'], true);
114
        $incident['stacktrace'] =
115
            json_decode($incident['stacktrace'], true);
116
117
        $this->set('incident', $incident);
118
    }
119
120
    private function _sendNotificationMail($reportId)
121
    {
122
        $this->Reports = TableRegistry::get('Reports');
0 ignored issues
show
Bug Best Practice introduced by
The property Reports does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Deprecated Code introduced by
The function Cake\ORM\TableRegistry::get() has been deprecated: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

122
        $this->Reports = /** @scrutinizer ignore-deprecated */ TableRegistry::get('Reports');

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

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

Loading history...
123
        $report = $this->Reports->findById($reportId)->all()->first()->toArray();
124
        $this->Reports->id = $reportId;
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Cake\ORM\Table.
Loading history...
125
126
        $viewVars = [
127
            'report' => $report,
128
            'project_name' => Configure::read('GithubRepoPath'),
129
            'incidents' => $this->Reports->getIncidents()->toArray(),
130
            'incidents_with_description' => $this->Reports->getIncidentsWithDescription(),
131
            'incidents_with_stacktrace' => $this->Reports->getIncidentsWithDifferentStacktrace(),
132
            'related_reports' => $this->Reports->getRelatedReports(),
133
            'status' => $this->Reports->status
134
        ];
135
        $viewVars = array_merge($viewVars, $this->_getSimilarFields($reportId));
136
137
        $this->Mailer->sendReportMail($viewVars);
0 ignored issues
show
Bug Best Practice introduced by
The property Mailer does not exist on App\Controller\IncidentsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
138
    }
139
140
    protected function _getSimilarFields($id)
141
    {
142
        $this->Reports->id = $id;
143
144
        $viewVars = [
145
            'columns' => TableRegistry::get('Incidents')->summarizableFields,
0 ignored issues
show
Deprecated Code introduced by
The function Cake\ORM\TableRegistry::get() has been deprecated: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

145
            'columns' => /** @scrutinizer ignore-deprecated */ TableRegistry::get('Incidents')->summarizableFields,

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

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

Loading history...
146
        ];
147
        $relatedEntries = [];
148
149
        foreach (TableRegistry::get('Incidents')->summarizableFields as $field) {
0 ignored issues
show
Deprecated Code introduced by
The function Cake\ORM\TableRegistry::get() has been deprecated: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

149
        foreach (/** @scrutinizer ignore-deprecated */ TableRegistry::get('Incidents')->summarizableFields as $field) {

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

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

Loading history...
150
            list($entriesWithCount, $totalEntries) =
151
                    $this->Reports->getRelatedByField($field, 25, true);
152
            $relatedEntries[$field] = $entriesWithCount;
153
            $viewVars["${field}_distinct_count"] = $totalEntries;
154
        }
155
156
        $viewVars['related_entries'] = $relatedEntries;
157
158
        return $viewVars;
159
    }
160
}
161