Skip to content

Commit b113434

Browse files
committed
Extract out generation code
1 parent 32aaf5c commit b113434

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

src/Requests/Payload.php

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,38 @@ public function __construct($request)
2929
}
3030

3131
/**
32-
* Returns the payload from a guzzle request.
32+
* Generates a payload with the provided properties
3333
*
34-
* @param \Psr\Http\Message\RequestInterface $request
35-
* An instance of the guzzle request to extract a payload from.
34+
* @param string $identifier
35+
* @param string $method
36+
* @param string $timestamp
37+
* @param string $uri
38+
* @param string $content
3639
*
3740
* @return string
38-
* The payload.
3941
*/
40-
protected function generateFromPsr7Request(Psr7Request $request) : string
41-
{
42-
$id = isset($this->request->getHeader('X-SIGNED-ID')[0]) ?
43-
$this->request->getHeader('X-SIGNED-ID')[0] : '';
44-
$timestamp = isset($this->request->getHeader('X-SIGNED-TIMESTAMP')[0]) ?
45-
$this->request->getHeader('X-SIGNED-TIMESTAMP')[0] : '';
46-
42+
private function generate(
43+
string $identifier,
44+
string $method,
45+
string $timestamp,
46+
string $uri,
47+
string $content
48+
) : string {
4749
$payload = [
48-
'id' => (string) $id,
49-
'method' => strtoupper($this->request->getMethod()),
50+
'id' => $identifier,
51+
'method' => strtoupper($method),
5052
'timestamp' => $timestamp,
51-
'uri' => rtrim((string) $this->request->getUri(), '/')
53+
'uri' => rtrim($uri, '/')
5254
];
5355

54-
if (is_null(json_decode((string) $this->request->getBody()))) {
56+
if (is_null(json_decode($content))) {
5557
$payload = array_merge($payload, [
56-
'content' => (string) $this->request->getBody()
58+
'content' => $content
5759
]);
5860
} else {
5961
$payload = array_merge($payload, [
6062
'content' => json_encode(
61-
json_decode((string) $this->request->getBody()),
63+
json_decode($content),
6264
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
6365
)
6466
]);
@@ -67,6 +69,31 @@ protected function generateFromPsr7Request(Psr7Request $request) : string
6769
return json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
6870
}
6971

72+
/**
73+
* Returns the payload from a guzzle request.
74+
*
75+
* @param \Psr\Http\Message\RequestInterface $request
76+
* An instance of the guzzle request to extract a payload from.
77+
*
78+
* @return string
79+
* The payload.
80+
*/
81+
protected function generateFromPsr7Request(Psr7Request $request) : string
82+
{
83+
$id = isset($this->request->getHeader('X-SIGNED-ID')[0]) ?
84+
$this->request->getHeader('X-SIGNED-ID')[0] : '';
85+
$timestamp = isset($this->request->getHeader('X-SIGNED-TIMESTAMP')[0]) ?
86+
$this->request->getHeader('X-SIGNED-TIMESTAMP')[0] : '';
87+
88+
return $this->generate(
89+
(string) $id,
90+
(string) $this->request->getMethod(),
91+
(string) $timestamp,
92+
(string) $this->request->getUri(),
93+
(string) $this->request->getBody()
94+
);
95+
}
96+
7097
/**
7198
* Retruns the payload from an illuminate request.
7299
*
@@ -81,27 +108,13 @@ protected function generateFromIlluminateRequest(IlluminateRequest $request) : s
81108
$id = $this->request->headers->get('X-SIGNED-ID', '');
82109
$timestamp = $this->request->headers->get('X-SIGNED-TIMESTAMP', '');
83110

84-
$payload = [
85-
'id' => (string) $id,
86-
'method' => strtoupper($this->request->getMethod()),
87-
'timestamp' => $timestamp,
88-
'uri' => rtrim((string) $this->request->fullUrl(), '/'),
89-
];
90-
91-
if (is_null(json_decode((string) $this->request->getContent()))) {
92-
$payload = array_merge($payload, [
93-
'content' => (string) $this->request->getContent()
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-
]);
102-
}
103-
104-
return json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
111+
return $this->generate(
112+
(string) $id,
113+
(string) $this->request->getMethod(),
114+
(string) $timestamp,
115+
(string) $this->request->fullUrl(),
116+
(string) $this->request->getContent()
117+
);
105118
}
106119

107120
/**

0 commit comments

Comments
 (0)