Skip to content

Commit 32aaf5c

Browse files
committed
Remove some code duplication
1 parent d50a023 commit 32aaf5c

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

src/Requests/Payload.php

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,27 @@ protected function generateFromPsr7Request(Psr7Request $request) : string
4444
$timestamp = isset($this->request->getHeader('X-SIGNED-TIMESTAMP')[0]) ?
4545
$this->request->getHeader('X-SIGNED-TIMESTAMP')[0] : '';
4646

47-
$string = json_decode((string) $this->request->getBody());
47+
$payload = [
48+
'id' => (string) $id,
49+
'method' => strtoupper($this->request->getMethod()),
50+
'timestamp' => $timestamp,
51+
'uri' => rtrim((string) $this->request->getUri(), '/')
52+
];
4853

49-
if (is_null($string)) {
50-
return json_encode([
51-
'id' => (string)$id,
52-
'method' => strtoupper($this->request->getMethod()),
53-
'timestamp' => $timestamp,
54-
'uri' => rtrim((string)$this->request->getUri(), '/'),
54+
if (is_null(json_decode((string) $this->request->getBody()))) {
55+
$payload = array_merge($payload, [
5556
'content' => (string) $this->request->getBody()
56-
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
57+
]);
58+
} else {
59+
$payload = array_merge($payload, [
60+
'content' => json_encode(
61+
json_decode((string) $this->request->getBody()),
62+
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
63+
)
64+
]);
5765
}
5866

59-
return json_encode([
60-
'id' => (string) $id,
61-
'method' => strtoupper($this->request->getMethod()),
62-
'timestamp' => $timestamp,
63-
'uri' => rtrim((string) $this->request->getUri(), '/'),
64-
'content' => json_encode(
65-
json_decode((string) $this->request->getBody()),
66-
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
67-
)
68-
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
67+
return json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
6968
}
7069

7170
/**
@@ -82,28 +81,27 @@ protected function generateFromIlluminateRequest(IlluminateRequest $request) : s
8281
$id = $this->request->headers->get('X-SIGNED-ID', '');
8382
$timestamp = $this->request->headers->get('X-SIGNED-TIMESTAMP', '');
8483

85-
$string = json_decode((string) $this->request->getContent());
84+
$payload = [
85+
'id' => (string) $id,
86+
'method' => strtoupper($this->request->getMethod()),
87+
'timestamp' => $timestamp,
88+
'uri' => rtrim((string) $this->request->fullUrl(), '/'),
89+
];
8690

87-
if (is_null($string)) {
88-
return json_encode([
89-
'id' => (string) $id,
90-
'method' => strtoupper($this->request->getMethod()),
91-
'timestamp' => $timestamp,
92-
'uri' => rtrim((string) $this->request->fullUrl(), '/'),
91+
if (is_null(json_decode((string) $this->request->getContent()))) {
92+
$payload = array_merge($payload, [
9393
'content' => (string) $this->request->getContent()
94-
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
94+
]);
95+
} else {
96+
$payload = array_merge($payload, [
97+
'content' => json_encode(
98+
json_decode((string)$this->request->getContent()),
99+
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
100+
)
101+
]);
95102
}
96103

97-
return json_encode([
98-
'id' => (string)$id,
99-
'method' => strtoupper($this->request->getMethod()),
100-
'timestamp' => $timestamp,
101-
'uri' => rtrim((string) $this->request->fullUrl(), '/'),
102-
'content' => json_encode(
103-
json_decode((string) $this->request->getContent()),
104-
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
105-
)
106-
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
104+
return json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
107105
}
108106

109107
/**

0 commit comments

Comments
 (0)