Completed
Push — master ( 07e3a8...9561cd )
by William
16:58 queued 14:23
created

IncidentsController::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 22
nc 4
nop 0
dl 0
loc 38
ccs 18
cts 18
cp 1
crap 4
rs 9.568
c 2
b 0
f 1
1
<?php
2
3
/**
4
 * Incidents controller handling incident creation and rendering.
5
 *
6
 * phpMyAdmin Error reporting server
7
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
8
 *
9
 * Licensed under The MIT License
10
 * For full copyright and license information, please see the LICENSE.txt
11
 * Redistributions of files must retain the above copyright notice.
12
 *
13
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
14
 * @license   https://opensource.org/licenses/mit-license.php MIT License
15
 *
16
 * @see      https://www.phpmyadmin.net/
17
 */
18
19
namespace App\Controller;
20
21
use Cake\Core\Configure;
22
use Cake\Http\Exception\NotFoundException;
23
use Cake\Http\Response;
24
use Cake\ORM\TableRegistry;
25
use const JSON_PRETTY_PRINT;
26
use const JSON_UNESCAPED_SLASHES;
27
use function __;
28
use function array_merge;
29
use function count;
30
use function in_array;
31
use function json_decode;
32
use function json_encode;
33
34
/**
35
 * Incidents controller handling incident creation and rendering.
36
 */
37
class IncidentsController extends AppController
38
{
39
    /** @var string[] */
40
    public $uses = [
41
        'Incident',
42
        'Notification',
43
    ];
44
45
    /** @var string */
46
    public $components = ['Mailer'];
47
48 4
    public function create(): ?Response
49
    {
50
        // Only allow POST requests
51 4
        $this->request->allowMethod(['post']);
52
53 4
        $bugReport = $this->request->input('json_decode', true);
54 4
        $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...
55
56 4
        if (count($result['incidents']) > 0
57 4
            && ! in_array(false, $result['incidents'])
58
        ) {
59
            $response = [
60 4
                'success' => true,
61 4
                'message' => 'Thank you for your submission',
62 4
                'incident_id' => $result['incidents'],        // Return a list of incident ids.
63
            ];
64
        } else {
65
            $response = [
66 4
                'success' => false,
67
                'message' => 'There was a problem with your submission.',
68
            ];
69
        }
70 4
        $this->autoRender = false;
71 4
        $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

71
        /** @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...
72 4
            'Content-Type' => 'application/json',
73
            'X-Content-Type-Options' => 'nosniff',
74
        ]);
75 4
        $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

75
        /** @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...
76 4
            json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
77
        );
78
79
        // For all the newly added reports,
80
        // send notification emails
81 4
        foreach ($result['reports'] as $report_id) {
82 4
            $this->sendNotificationMail($report_id);
83
        }
84
85 4
        return $this->response;
86
    }
87
88 4
    public function json(?string $id): ?Response
89
    {
90 4
        if (empty($id)) {
91
            throw new NotFoundException(__('Invalid Incident'));
92
        }
93
94 4
        $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...
95 4
        $incident = $this->Incidents->findById($id)->all()->first();
96 4
        if (! $incident) {
97
            throw new NotFoundException(__('Invalid Incident'));
98
        }
99
100 4
        $incident['full_report'] =
101 4
            json_decode($incident['full_report'], true);
102 4
        $incident['stacktrace'] =
103 4
            json_decode($incident['stacktrace'], true);
104
105 4
        $this->autoRender = false;
106 4
        $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

106
        /** @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...
107
108 4
        return $this->response;
109
    }
110
111 4
    public function view(?string $incidentId): void
112
    {
113 4
        if (empty($incidentId)) {
114
            throw new NotFoundException(__('Invalid Incident'));
115
        }
116
117 4
        $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...
118 4
        if (! $incident) {
119
            throw new NotFoundException(__('Invalid Incident'));
120
        }
121
122 4
        $incident['full_report'] =
123 4
            json_decode($incident['full_report'], true);
124 4
        $incident['stacktrace'] =
125 4
            json_decode($incident['stacktrace'], true);
126
127 4
        $this->set('incident', $incident);
128 4
    }
129
130 4
    private function sendNotificationMail(int $reportId): void
131
    {
132 4
        $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...
133 4
        $report = $this->Reports->findById($reportId)->all()->first()->toArray();
134 4
        $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...
135
136
        $viewVars = [
137 4
            'report' => $report,
138 4
            'project_name' => Configure::read('GithubRepoPath'),
139 4
            'incidents' => $this->Reports->getIncidents()->toArray(),
140 4
            'incidents_with_description' => $this->Reports->getIncidentsWithDescription(),
141 4
            'incidents_with_stacktrace' => $this->Reports->getIncidentsWithDifferentStacktrace(),
142 4
            'related_reports' => $this->Reports->getRelatedReports(),
143 4
            'status' => $this->Reports->status,
144
        ];
145 4
        $viewVars = array_merge($viewVars, $this->getSimilarFields($reportId));
146
147 4
        $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...
148 4
    }
149
150 4
    protected function getSimilarFields(int $id): array
151
    {
152 4
        $this->Reports->id = $id;
153
154
        $viewVars = [
155 4
            'columns' => TableRegistry::getTableLocator()->get('Incidents')->summarizableFields,
156
        ];
157 4
        $relatedEntries = [];
158
159 4
        foreach (TableRegistry::getTableLocator()->get('Incidents')->summarizableFields as $field) {
160
            [$entriesWithCount, $totalEntries] =
161 4
                    $this->Reports->getRelatedByField($field, 25, true);
162 4
            $relatedEntries[$field] = $entriesWithCount;
163 4
            $viewVars["${field}_distinct_count"] = $totalEntries;
164
        }
165
166
        $viewVars['related_entries'] = $relatedEntries;
167
168
        return $viewVars;
169
    }
170
}
171