Passed
Push — master ( 2acbdd...490d36 )
by William
07:42
created

IncidentsController::_getSimilarFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 19
ccs 8
cts 8
cp 1
crap 2
rs 9.9
c 0
b 0
f 0
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 1
    public function create()
39
    {
40
        // Only allow POST requests
41 1
        $this->request->allowMethod(['post']);
42
43 1
        $bugReport = $this->request->input('json_decode', true);
44 1
        $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 1
        if (count($result['incidents']) > 0
47 1
            && ! in_array(false, $result['incidents'])
48
        ) {
49
            $response = [
50 1
                'success' => true,
51 1
                'message' => 'Thank you for your submission',
52 1
                'incident_id' => $result['incidents'],        // Return a list of incident ids.
53
            ];
54
        } else {
55
            $response = [
56 1
                'success' => false,
57
                'message' => 'There was a problem with your submission.',
58
            ];
59
        }
60 1
        $this->autoRender = false;
61 1
        $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 1
            'Content-Type' => 'application/json',
63
            'X-Content-Type-Options' => 'nosniff',
64
        ]);
65 1
        $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 1
            json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
67
        );
68
69
        // For all the newly added reports,
70
        // send notification emails
71 1
        foreach ($result['reports'] as $report_id) {
72 1
            $this->_sendNotificationMail($report_id);
73
        }
74
75 1
        return $this->response;
76
    }
77
78 1
    public function json($id)
79
    {
80 1
        if (! isset($id) || ! $id) {
81
            throw new NotFoundException(__('Invalid Incident'));
82
        }
83
84 1
        $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 1
        $incident = $this->Incidents->findById($id)->all()->first();
86 1
        if (! $incident) {
87
            throw new NotFoundException(__('Invalid Incident'));
88
        }
89
90 1
        $incident['full_report'] =
91 1
            json_decode($incident['full_report'], true);
92 1
        $incident['stacktrace'] =
93 1
            json_decode($incident['stacktrace'], true);
94
95 1
        $this->autoRender = false;
96 1
        $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 1
        return $this->response;
99
    }
100
101 1
    public function view($incidentId)
102
    {
103 1
        if (! isset($incidentId) || ! $incidentId) {
104
            throw new NotFoundException(__('Invalid Incident'));
105
        }
106
107 1
        $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 1
        if (! $incident) {
109
            throw new NotFoundException(__('Invalid Incident'));
110
        }
111
112 1
        $incident['full_report'] =
113 1
            json_decode($incident['full_report'], true);
114 1
        $incident['stacktrace'] =
115 1
            json_decode($incident['stacktrace'], true);
116
117 1
        $this->set('incident', $incident);
118 1
    }
119
120 1
    private function _sendNotificationMail($reportId)
121
    {
122 1
        $this->Reports = TableRegistry::getTableLocator()->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...
123 1
        $report = $this->Reports->findById($reportId)->all()->first()->toArray();
124 1
        $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 1
            'report' => $report,
128 1
            'project_name' => Configure::read('GithubRepoPath'),
129 1
            'incidents' => $this->Reports->getIncidents()->toArray(),
130 1
            'incidents_with_description' => $this->Reports->getIncidentsWithDescription(),
131 1
            'incidents_with_stacktrace' => $this->Reports->getIncidentsWithDifferentStacktrace(),
132 1
            'related_reports' => $this->Reports->getRelatedReports(),
133 1
            'status' => $this->Reports->status
134
        ];
135 1
        $viewVars = array_merge($viewVars, $this->_getSimilarFields($reportId));
136
137 1
        $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 1
    }
139
140 1
    protected function _getSimilarFields($id)
141
    {
142 1
        $this->Reports->id = $id;
143
144
        $viewVars = [
145 1
            'columns' => TableRegistry::getTableLocator()->get('Incidents')->summarizableFields,
146
        ];
147 1
        $relatedEntries = [];
148
149 1
        foreach (TableRegistry::getTableLocator()->get('Incidents')->summarizableFields as $field) {
150
            list($entriesWithCount, $totalEntries) =
151 1
                    $this->Reports->getRelatedByField($field, 25, true);
152 1
            $relatedEntries[$field] = $entriesWithCount;
153 1
            $viewVars["${field}_distinct_count"] = $totalEntries;
154
        }
155
156
        $viewVars['related_entries'] = $relatedEntries;
157
158
        return $viewVars;
159
    }
160
}
161