Skip to content

Commit af26d5c

Browse files
committed
Improve Incidents creation
1 parent e8cb0bf commit af26d5c

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ parameters:
249249
-
250250
message: '#^Parameter \#1 \$string of method Cake\\Http\\Response\:\:withStringBody\(\) expects string\|null, string\|false given\.$#'
251251
identifier: argument.type
252-
count: 2
252+
count: 3
253253
path: src/Controller/IncidentsController.php
254254

255255
-

src/Controller/IncidentsController.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,28 @@ public function initialize(): void
6464

6565
public function create(): ?Response
6666
{
67+
$this->disableAutoRender();
68+
$this->response = $this->response
69+
->withHeader('Content-Type', 'application/json')
70+
->withHeader('X-Content-Type-Options', 'nosniff');
71+
6772
// Only allow POST requests
6873
$this->request->allowMethod(['post']);
6974

7075
$requestBody = (string) $this->request->getBody();
7176
$bugReport = json_decode($requestBody, true);
77+
78+
if ($bugReport === null || $bugReport === false || $requestBody === '') {
79+
$response = ['success' => false];
80+
81+
$this->response = $this->response
82+
->withStringBody(
83+
json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
84+
);
85+
86+
return $this->response;
87+
}
88+
7289
$result = $this->Incidents->createIncidentFromBugReport($bugReport);
7390

7491
$sentryConfig = Configure::read('Forwarding.Sentry');
@@ -81,20 +98,12 @@ public function create(): ?Response
8198
count($result['incidents']) > 0
8299
&& ! in_array(false, $result['incidents'])
83100
) {
84-
$response = [
85-
'success' => true,
86-
];
101+
$response = ['success' => true];
87102
} else {
88-
$response = [
89-
'success' => false,
90-
];
103+
$response = ['success' => false];
91104
}
92105

93-
$this->disableAutoRender();
94-
95106
$this->response = $this->response
96-
->withHeader('Content-Type', 'application/json')
97-
->withHeader('X-Content-Type-Options', 'nosniff')
98107
->withStringBody(
99108
json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
100109
);

0 commit comments

Comments
 (0)